Loop on M query - powerbi

My table looks like this:
I want it to look like this:
I did it with :
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfL0s/b0sQ4NBjIDnK1DEpNyUkusAzLy81KBIkZAbKwUqxMNZoUGW3v6AWk0RaZghbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, market = _t, device = _t, userCount = _t, Num = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"market", type text}, {"device", type text}, {"userCount", Int64.Type}}), #"Transposed Table" = Table.Transpose(#"Changed Type"), #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Transposed Table", {{"Column1", type text}}, "en-US"),"Column1",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Column1.1", "Column1.2", "Column1.3"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}}), #"Split Column by Delimiter1" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type1", {{"Column2", type text}}, "en-US"),"Column2",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Column2.1", "Column2.2"}), #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column2.1", type text}, {"Column2.2", type text}}), #"Transposed Table1" = Table.Transpose(#"Changed Type2"), #"Filled Down" = Table.FillDown(#"Transposed Table1",{"Column3", "Column4", "Column5", "Column1"}), #"Changed Type3" = Table.TransformColumnTypes(#"Filled Down",{{"Column4", Int64.Type}, {"Column5", Int64.Type}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type3",{{"Column1", "ID"}, {"Column2", "Market"}, {"Column3", "Device"}, {"Column4", "UserCount"}, {"Column5", "Num"}}) in #"Renamed Columns"
I wanted to split the multiple columns, by saving the order of each value with the semicolon:
USERCOUNT of IN and PC is 2
USERCOUNT of Us and Tablet is 5
USERCOUNT US and PC is blank
This code is easy when you have 2 rows transposed to 2 columns but what if I don't know how many rows I have?
I want to create a loop to do the function on each column (after first transposed).

you can use this code which provides a dynamic solution:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfL0s/b0sQ4NBjIDnK1DEpNyUkusAzLy81KBIkZAbKwUqxMNZoUGW3v6AWk0RaZghbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, market = _t, device = _t, userCount = _t, Num = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"market", type text}, {"device", type text}, {"userCount", Int64.Type}}),
ToLists = Table.TransformColumns(#"Changed Type",{{"market", each Text.Split(_, ";")}, {"device", each Text.Split(_, ";")}}),
#"Added Custom" = Table.AddColumn(ToLists, "Custom", each Table.FromColumns({[market], [device]})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"market", "device"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1", "Column2"}, {"Market", "Device"})
in
#"Expanded Custom"

Related

Power Query : Split Table Every n Columns

I have many columns of same table
Table A
Monthly Plan
Weekly plan
Actual
Monthly_Plan1
Weekly_plan 2
Actual_3
A
B
C
D
E
F
I want them as :
Monthly Plan
Weekly plan
Actual
A
B
C
D
E
F
I can not create a separate table and append it because there are so many columns and I cant create too many tables.
Here you go.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclRQ0lFyAhHOIMIFRLiCCDel2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Monthly Plan " = _t, #"Weekly plan " = _t, #"Actual " = _t, #"Monthly_Plan1 " = _t, #"Weekly_plan 2 " = _t, Actual_3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Monthly Plan ", type text}, {"Weekly plan ", type text}, {"Actual ", type text}, {"Monthly_Plan1 ", type text}, {"Weekly_plan 2 ", type text}, {"Actual_3", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Split( Record.ToList( _), 3)),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Other Columns", "Custom"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), "|"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Monthly", "Weekly", "Actual"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Monthly", type text}, {"Weekly", type text}, {"Actual", type text}})
in
#"Changed Type1"
Add a custom column
Remove other columns
Expand
Extract values concatenating with a |
Split the column and rename
If you don't care about row order this also works
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Combo = List.Split(Table.ColumnNames(Source),3),
#"Added Custom" =List.Accumulate(Combo, #table({"Column1"}, {}),(state,current)=> state & Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, current)),1))
in #"Added Custom"

How to count duplicate values with multiple columns in Power BI by row wise

Consider:
I have four columns (A1, A2, A3 & A4) and I want to count the same/duplicate values in these four columns by grouping Index column.
For example, if "Index 1" has found the value in "A1" and the same value exists next to A2 column then it should remove it. If it’s not next to A1 column then it should stay. For example, "1 index" can take only one unique value in the four columns.
I would use Pivot and Unpivot to get this result.
Starting with a mock dataset:
I was able to transform it to this:
Here is the advanced query for your review:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZDdCoMwDIXfpddezG3u51mKF9FJG2Zt0Anu7ZfUdWQUSs7hfIeU1lpTm8rASB5Yv6etrDmqWGNhJ5V1w0ujc4lyQ3BTxA5C+OGLCjCW/MrmCUSgdFozvbEZIXQPicP6x+5sNswjBuznOCknnfrAAQmffeS5oEtX75oa8lnkcX8wLe+0YXBM2w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, A1 = _t, A2 = _t, A3 = _t, A4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"A1", type text}, {"A2", type text}, {"A3", type text}, {"A4", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each [Value] <> null and [Value] <> ""),
#"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Value", "ID"}),
#"Pivoted Column" = Table.Pivot(#"Removed Duplicates", List.Distinct(#"Removed Duplicates"[Attribute]), "Attribute", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,"",Replacer.ReplaceValue,{"A1", "A2", "A3", "A4"})
in
#"Replaced Value"
The idea is that you will unpivot your columns so that it is possible to take advantage of 'remove duplicates' functionality. Then pivot the dataset back to its original form.
If you want to close the gaps by shifting values to the left, you will need to do a little more work. While the data is unpivoted, create a new index over the subgroups. This is an intermediate level task, Curbal has a good example (https://www.youtube.com/watch?v=7CqXdSEN2k4). Discard your 'Attribute' column from the unpivot and use your new subgroup index instead.
Here's the advanced editor for the shift:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZDdCoMwDIXfpddezG3u51mKF9FJG2Zt0Anu7ZfUdWQUSs7hfIeU1lpTm8rASB5Yv6etrDmqWGNhJ5V1w0ujc4lyQ3BTxA5C+OGLCjCW/MrmCUSgdFozvbEZIXQPicP6x+5sNswjBuznOCknnfrAAQmffeS5oEtX75oa8lnkcX8wLe+0YXBM2w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, A1 = _t, A2 = _t, A3 = _t, A4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"A1", type text}, {"A2", type text}, {"A3", type text}, {"A4", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each [Value] <> null and [Value] <> ""),
#"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Value", "ID"}),
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"ID"}, {{"Data", each Table.AddIndexColumn(_, "AttributeRenumber", 1, 1), type table [ID=nullable number, Attribute=text, Value=text, AttributeRenumber=text]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"ID"}),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"ID", "Attribute", "Value", "AttributeRenumber"}, {"ID", "Attribute", "Value", "AttributeRenumber"}),
#"Add Prefix" = Table.TransformColumns(#"Expanded Data",{{"AttributeRenumber", each "A" & Number.ToText(_) , type text}}),
#"Removed Columns1" = Table.RemoveColumns(#"Add Prefix",{"Attribute"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[AttributeRenumber]), "AttributeRenumber", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,"",Replacer.ReplaceValue,{"A1", "A2", "A3"})
in
#"Replaced Value"

Power Query Table Transformation

I am using power query in Excel. I have following table generated from a raw SQL query.
ABC_MONTH
ABC_YEAR
XYZ_MONTH
XYZ_YEAR
10
20
30
40
I would like to transform the above table into the following one in power query or tansformations. Kindly help.
NAME
MONTH
YEAR
ABC
10
20
XYZ
30
40
First unpivot all the columns to get this:
Split the Attribute column by the left-most underscore:
To get this:
Then pivot Attribute.2 and Value:
To get the desired output (after renaming Attribute.1 to NAME):
The M code to try:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRQ0lEyAhHGIMLEQCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ABC_MONTH = _t, ABC_YEAR = _t, XYZ_MONTH = _t, XYZ_YEAR = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ABC_MONTH", Int64.Type}, {"ABC_YEAR", Int64.Type}, {"XYZ_MONTH", Int64.Type}, {"XYZ_YEAR", Int64.Type}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"_"}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute.2]), "Attribute.2", "Value", List.Sum),
#"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Attribute.1", "NAME"}})
in
#"Renamed Columns"

Converting column type to time when over 24 hours

I am trying to change the format of a column from text to time in Power Bi:
When I change the format to time, anything that is over 24 hours shows as an error:
Does anyone know how to resolve this?
You can try this below Advanced Editor code for your purpose.
Conditions Values like (73:30) required a delimiter : for this code. You can also adjust the code if different delimiter in use.
Output will in dd:hh:mm:ss
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjSwMjBQitWJVjKGs8yNrYyBrFgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t]),
#"Duplicated Column" = Table.DuplicateColumn(Source, "Time", "Time - Copy"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Time - Copy", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Time - Copy.1", "Time - Copy.2"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Time", type text}, {"Time - Copy.1", Int64.Type}, {"Time - Copy.2", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Time - Copy.1", "hour"}, {"Time - Copy.2", "minutes"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Second", each ([hour]*60*60) + ([minutes]*60)),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each #duration(0,0,0,[Second])),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom", type duration}})
in
#"Changed Type1"
Input-
Output-

How to remove duplicate values in a list in powerbi report dataset?

Hope you are staying safe. I have a column in my powerbi report dataset which holds list values in a column.
Id Name
1 kevin,yona,rachel,kevin
2 bruce,miller,kim
3 adam,rita,adam,adam
As you can see there are duplicate values in the list on Name column. I wanted to write a query which will remove those duplicates and and keep one occurences. the result set i want is like this
Id Name
1 yona,rachel,kevin
2 bruce,miller,kim
3 adam,rita
Any ideas? thanks
You could split, remove duplicates and group by again.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Hck7CoBADIThu6SeRj1O2CKuAcO+IKjg7V3TDD/zMdNCoKKPdbyjC1zyqRXxUALTOn33Oyua1aqOYi1gmyCHNLhdgqh/KKUP", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Column2", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column2"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column2", type text}}),
#"Removed Duplicates" = Table.Distinct(#"Changed Type1"),
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Column1"}, {{"Rows", each _, type table [Column1=number, Column2=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [Rows][Column2]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Rows"})
in
#"Removed Columns"