Power Query — это мощный инструмент преобразования и анализа данных, доступный в Microsoft Excel и Power BI. Он позволяет пользователям подключаться к различным источникам данных, манипулировать и преобразовывать данные, а также выполнять сложные задачи анализа данных. В этой статье мы рассмотрим различные методы добавления строк к вашим данным с помощью Power Query, а также приведем примеры кода.
Метод 1: использование Table.InsertRows
let
// Load the source table
Source = Excel.CurrentWorkbook(){[Name="SourceTable"]}[Content],
// Create a new row to append
NewRow = [Column1 = "New Value", Column2 = 123, Column3 = "Other Value"],
// Append the new row to the source table
Result = Table.InsertRows(Source, Source.RowCount + 1, {NewRow})
in
Result
Метод 2: использование Table.FromRecords
let
// Load the source table
Source = Excel.CurrentWorkbook(){[Name="SourceTable"]}[Content],
// Create a new row to append
NewRow = [Column1 = "New Value", Column2 = 123, Column3 = "Other Value"],
// Convert the new row into a table
NewTable = Table.FromRecords({NewRow}),
// Combine the source table with the new table
Result = Table.Combine({Source, NewTable})
in
Result
Метод 3: использование Table.ExpandTableColumn
let
// Load the source table
Source = Excel.CurrentWorkbook(){[Name="SourceTable"]}[Content],
// Create a new row to append
NewRow = [Column1 = "New Value", Column2 = 123, Column3 = "Other Value"],
// Convert the new row into a table
NewTable = Table.FromRecords({NewRow}),
// Expand the new table into separate columns
ExpandedTable = Table.ExpandTableColumn(NewTable, "Column1", {"Column1", "Column2", "Column3"}),
// Combine the source table with the expanded table
Result = Table.Combine({Source, ExpandedTable})
in
Result
Метод 4: использование List.Combine и Record.ToTable
let
// Load the source table
Source = Excel.CurrentWorkbook(){[Name="SourceTable"]}[Content],
// Create a new row to append
NewRow = [Column1 = "New Value", Column2 = 123, Column3 = "Other Value"],
// Convert the new row into a list
NewList = {NewRow},
// Convert the list into a table
NewTable = Record.ToTable(List.Combine(NewList)),
// Combine the source table with the new table
Result = Table.Combine({Source, NewTable})
in
Result
Power Query предоставляет несколько методов добавления строк в данные. Независимо от того, предпочитаете ли вы использовать встроенные функции, такие как Table.InsertRows, или такие функции, как Table.Combine и Table.ExpandTableColumn, Power Query предлагает гибкость и удобство для задач манипулирования данными. Используя эти методы, вы можете эффективно включать новые строки в свои наборы данных, расширяя возможности анализа данных в Excel и Power BI.
Не забудьте сохранить код Power Query и обновлять данные при каждом обновлении или изменении, чтобы обеспечить точность и актуальность анализа.