I have an excel file that needs to be transposed from multiple columns into table format, but am having trouble merging the rows in Power Query.
Sample Data
Expected Outcome
Thanks a lot for your help in advance.
Before loading into PowerQuery, insert a row in your excelsheet and concatenate the headers in the value columns, using a delimiter. You can use the TEXTJOIN function to do this if you use office365. The result looks something like this (I did not copy all your data):
Import this tabel into PowerQuery and perform the following steps:
Remove top 3 rows
Use first row as headers
Select the first 3 columns
Unpivot other columns (dropdown menu unpivot columns on Transform Tab)
Select [Attribute] column
Split column by delimiter (Semicolon)
The script looks like this.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type any}, {"Column5", type any}}),
#"Removed Top Rows" = Table.Skip(#"Changed Type",3),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Actual;jun;FY-2017", Int64.Type}, {"Actual;jul;FY-2017", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1", "Column2", "Column3"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Attribute.1", "Attribute.2", "Attribute.3"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}, {"Attribute.3", type text}})
in
#"Changed Type2"
The result:
Related
I tried to split using custom delimiter per column. I cannot figure out how can I split both column at once because every date in a row is assigned for every user id. I need to split it in a row.
You can try
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
RemoveExtraCharacter = Table.TransformColumns(Source,{{"dates", each Text.Remove(_,{"]","[","}","{",""""}), type text}, {"user_id", each Text.Remove(_,{"]","[","}","{",""""}), type text}}),
#"Transposed Table" = Table.Transpose(RemoveExtraCharacter),
#"Merged Columns" = Table.CombineColumns(#"Transposed Table", Table.ColumnNames(#"Transposed Table"), Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Merged Columns", "Merged", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv)),
#"Transposed Table1" = Table.Transpose(#"Split Column by Delimiter"),
#"Changed Type" = Table.TransformColumnTypes(#"Transposed Table1",{{"Column1", type date}})
in #"Changed Type"
I have sample data like below and I am trying to use PowerQuery to transpose it into different shape.
Here is my data:
Identifier Id
Account Type 1
Account Type 2
Account Type 3
Here is what I need:
Identifier Column.1 Column.2 Column.3
Account Type 1 2 3
I tried all combinations of Transpose + Unpivot but nothing worked.
You can Group by Identifier; then do a custom Text Aggregation which you can split into columns:
let
Source = Excel.CurrentWorkbook(){[Name="Table25"]}[Content],
//type the ID column as text for later purposes
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Identifier", type text}, {"Id", type text}}),
//group by Identifier, then custom Text aggregation with delimiter
#"Grouped Rows" = Table.Group(#"Changed Type", {"Identifier"}, {
{"Id",each Text.Combine([Id],";")}}),
//split the column by the delimiter, and set the data types
#"Split Column by Delimiter" = Table.SplitColumn(#"Grouped Rows", "Id", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Id.1", "Id.2", "Id.3"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Id.1", Int64.Type}, {"Id.2", Int64.Type}, {"Id.3", Int64.Type}})
in
#"Changed Type1"
How about this?
Method1: If there will only be one identifier type, then Add column... index column .... Then click select the index column and use transform...pivot column... and select ID as the values column, advanced options Don't Aggregate
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Index", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Index", {{"Index", type text}}, "en-US")[Index]), "Index", "Id")
in #"Pivoted Column"
Method2: If you plan to have different identifiers, then you need something a bit more complex. This adds the index within each group. Then you can pivot
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Identifier"}, {{"data", each Table.AddIndexColumn(_, "Index", 1, 1), type table}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Id", "Index"}, {"Id", "Index"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded data", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded data", {{"Index", type text}}, "en-US")[Index]), "Index", "Id", List.Sum)
in #"Pivoted Column"
I have a table in power bi query with dates
01.01.2020
02.01.2020
and so on..
I need to duplicate this table and replace values 01.01.2020 into 20200101 and so on. Is there an obvious, easy way for this?
First option:
Here is the simplest option I found:
Create a custom column and apply "Text.Reverse" to your column
Create a custom column and apply to the newly created "Text.Remove" for "." which will remove the "." of your string.
Here is what you will get, with "reverse date" as your column in the reverse order, and "reverse date without point" as the second column without the point.
Here is the M code:
#"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
#"Changed Type3" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type text}}),
#"Added Custom3" = Table.AddColumn(#"Changed Type3", "reverse date", each Text.Reverse([Date])),
#"Added Custom4" = Table.AddColumn(#"Added Custom3", "reverse date witout point", each Text.Remove([reverse date], {"."}))
Second option:
Here is a second option, which is longer:
Break down your column in three distinct columns with "." as delimiter
Add new columns with padding zero to day and months (I called them "month with zero" and "day with zero")
Concatenate
and you get you result!
Here is my starting point:
Here is the first step, "breaking the column" in "columns":
Here is the custom column with zero padding:
Here is how you concatenate:
Here is the M code:
#"Split Column by Delimiter" = Table.SplitColumn(#"Promoted Headers", "Date", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Date.1", "Date.2", "Date.3"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Date.1", Int64.Type}, {"Date.2", Int64.Type}, {"Date.3", Int64.Type}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Date.1", type text}, {"Date.2", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Date.1", "Day"}, {"Date.2", "Month"}, {"Date.3", "Year"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Month with zero", each Text.PadStart(Text.From([Month]),2,"0")),
#"Added Custom2" = Table.AddColumn(#"Added Custom", "Day with zero", each Text.PadStart(Text.From([Day]),2,"0")),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Day", "Month"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns",{{"Year", type text}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type2", "New Date", each [Year] & [Month with zero] & [Day with zero])
in
#"Added Custom1"
I'm looking for some Power Query help. I have a huge set of sales data for 40k products over one year. For each product on each day I need to add a 28 day sales column.
I essentially want to do a sumifs like the below but in M.
=SUMIFS([SALES],[Product Code],[This Product Code],[Date],<=[This Date],[Date],>=[This Date]-28))
Try this then, it should work but would likely do so at a crawl
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
TotalAmountAdded = Table.AddColumn(Source, "Total Amount", (i) => List.Sum(Table.SelectRows(Source, each ([Product Code] = i[Product Code] and [Date]<=i[Date] and [Date]>=Date.AddDays(i[Date],-28)))[Sales]), type number )
in TotalAmountAdded
Add a custom column with date logic (based on your sample sumif formula), filter the new column to get the relevant rows, then group by product code and sum Sales. Assuming source data is in Table1 with three columns (Sales,Product Code, Date) the code would be
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "AddMe", each if [Date]<=DateTime.Date(DateTime.LocalNow()) and [Date]>=Date.AddDays(DateTime.Date(DateTime.LocalNow()),-28) then 1 else 0),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([AddMe] = 1)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Product Code"}, {{"ProductSales", each List.Sum([Sales]), type number}})
in #"Grouped Rows"
I have this table:
Which I'd like to change to this:
As you can see, I want to collect all the related entries from all of the P1 rows, across all columns, into a single column under P1, and do the same for P2 and P3 related entries.
Is there a simple way to do this in PowerQuery / M?
Yes:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"P", type text}, {"15", Int64.Type}, {"25", Int64.Type}, {"35", Int64.Type}, {"45", Int64.Type}, {"55", Int64.Type}, {"65", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"P"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"P"}, {{"AllData", each _, type table}}),
TablesToLists = Table.TransformColumns(#"Grouped Rows",{{"AllData", each _[Value]}}),
#"Transposed Table" = Table.Transpose(TablesToLists),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
TableFromColumns = Table.FromColumns(Record.FieldValues(#"Promoted Headers"{0}),Table.ColumnNames(#"Promoted Headers"))
in
TableFromColumns