I'm trying to transform a single cell in a column into several rows. What technique or DAX should I use in PowerBI to achieve the result.
The table is details are given below,
+------------------------------------------------+----------------+
| Time | Status |
+------------------------------------------------+----------------+
| TimeStamp (2019-01-02, 2019-01-03, 2019-01-04) | (Yes, Yes, No) |
+------------------------------------------------+----------------+
I wanted a output like this,
+------------+----------+
| Time | Status |
+------------+----------+
| 2019-01-02 | Yes |
| 2019-01-03 | Yes |
| 2019-01-04 | No |
+------------+----------+
I have tried several solution and not able to attain a conclusion on PowerBI.
I have used both powerquery and DAX to solve this problem
First I have created book1 to get the status column and split column by (,) using row delimited option instead of column delimited and added index to it. Then book2 table created to get only the Time column and split column by (,) using same row delimited option and added index to it.
Book 1: Powerquery
let
Source = Csv.Document(File.Contents("C:\Users\PremChand\Desktop\stack\Book1.csv"),Delimiter=",", Columns=2, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Replaced Value" = Table.ReplaceValue(#"Promoted Headers","TimeStamp","",Replacer.ReplaceText,{"Time"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","(","",Replacer.ReplaceText,{"Time", "Status"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",")","",Replacer.ReplaceText,{"Time", "Status"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value2", {{"Status", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Status"),
#"Added Index" = Table.AddIndexColumn(#"Split Column by Delimiter", "Index", 0, 1),
#"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Time"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Index", "Index1"}})
in
#"Renamed Columns"
Book 2: Powerquery
let
Source = Csv.Document(File.Contents("C:\Users\PremChand\Desktop\stack\Book1.csv"),[Delimiter=",", Columns=2, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Replaced Value" = Table.ReplaceValue(#"Promoted Headers","TimeStamp","",Replacer.ReplaceText,{"Time"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","(","",Replacer.ReplaceText,{"Time", "Status"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",")","",Replacer.ReplaceText,{"Time", "Status"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value2", {{"Time", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Time"),
#"Added Index" = Table.AddIndexColumn(#"Split Column by Delimiter", "Index", 0, 1),
#"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Status"})
in
#"Removed Columns"
Once book1 and book2 has created relationship has created for both Index column. Then the new output table has created using DAX to join book1 and book2.
Below DAX used to join two tables:
Output = NATURALLEFTOUTERJOIN(Book2,Book1)
The Output table consist of Time, Status and Index and Index1 column you can select only Time and status column to show in a table.
The easiest is to use Power Query. Open the advanced editor and past the code below. You need to edit the part where you get the data:
let
GetData = (test as record) => let
textTimeStamp = Text.BetweenDelimiters(test[TimeStamp], "(", ")"),
tableTimeStamp = Table.FromList(Function.Invoke(Splitter.SplitTextByDelimiter(","), {textTimeStamp}),null,{"TimeStamp"}),
tableTimeStampIndex = Table.AddIndexColumn(tableTimeStamp, "Index"),
textAnswer = Text.BetweenDelimiters(test[Answer], "(", ")"),
tableAnswer = Table.FromList( Function.Invoke(Splitter.SplitTextByDelimiter(","), {textAnswer}),null,{"Answer"}),
tableAnswerIndex = Table.AddIndexColumn(tableAnswer, "Index"),
joinedTable = Table.Join(tableTimeStampIndex, "Index", tableAnswerIndex, "Index"),
removeIndex = Table.RemoveColumns(joinedTable,{"Index"})
in
removeIndex,
TimeSplit = let
Source = Csv.Document(File.Contents("C:\Users\...\Documents\TimeSplit.csv"),[Delimiter=",", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true])
in
#"Promoted Headers",
#"Invoked Custom Function" = Table.AddColumn(TimeSplit, "ToList", each GetData(_)),
#"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"TimeStamp", "Answer"}),
#"Expanded ToList" = Table.ExpandTableColumn(#"Removed Columns", "ToList", {"TimeStamp", "Answer"}, {"TimeStamp", "Answer"})
in
#"Expanded ToList"
Related
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"
I have 2 tables in Power BI (TableA and TableB).
In tableA I have logins and I want to find the number of ID from tableB but in tableB the login is in a text. Ex:
tableA:
tableB:
Split TableB Logins by comma into new rows.
Then Join the two tables.
let
//Read in Table B
Source = Excel.CurrentWorkbook(){[Name="TableB"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Logins", type text}, {"ID", Int64.Type}}),
//Split logins column by the comma into rows
//Then TRIM to get rid of leading/trailing spaces
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Logins", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Logins"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Logins", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type1",{{"Logins", Text.Trim, type text}}),
//Read in Table A and JOIN with B
Source2 = Excel.CurrentWorkbook(){[Name="TableA"]}[Content],
#"Changed Type2" = Table.TransformColumnTypes(Source2,{{"Logins", type text}}),
join=Table.NestedJoin(#"Changed Type2","Logins",#"Trimmed Text","Logins","Joined",JoinKind.LeftOuter),
//expand the joined table with ID only
#"Expanded Joined" = Table.ExpandTableColumn(join, "Joined", {"ID"}, {"ID"})
in
#"Expanded Joined"
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"
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"
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"