Forum Discussion
DAX Help
- 5 years ago
I'll post the code (which you can place in the Advanced Editor in Power Query). This'll either confound you or inspire you. Let's hope it's the latter.
Basically you'll be unpivotting the column pairs. To do this you need consistency in the naming of pairs e.g. Jan21 for 2 columns or Jan for 2 columns.
Here's the code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY0xDoAgDEXvQhwZLIhx1UEP4EiYCAOLGpT729KoDA5NX/Jf/rdWjEKKOactXjkFZMBrdNviU4QdopPsLWn3IcVwIg8UDqSBJgT4vPWIvkiGEkNSx7VFmf4mVdEY+8+rJ0E9Q8ilGqrGd5QtyjUXC+du", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SuperMarket = _t, #"Goods Purchased" = _t, #"Jan21 Quantity" = _t, #"Jan Expenditure" = _t, #"Feb21 Quantity" = _t, #"Feb Expenditure" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"SuperMarket", type text}, {"Goods Purchased", type text}, {"Jan21 Quantity", Int64.Type}, {"Jan Expenditure", type text}, {"Feb21 Quantity", Int64.Type}, {"Feb Expenditure", type text}}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"SuperMarket", "Goods Purchased"}, "Attribute", "Value"), #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other 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}}), #"Replaced Value" = Table.ReplaceValue(#"Changed Type1","21","",Replacer.ReplaceText,{"Attribute.1"}), #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Attribute.2]), "Attribute.2", "Value"), #"Replaced Value1" = Table.ReplaceValue(#"Pivoted Column","$","",Replacer.ReplaceText,{"Expenditure"}), #"Changed Type2" = Table.TransformColumnTypes(#"Replaced Value1",{{"Expenditure", Int64.Type}, {"Quantity", Int64.Type}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type2",{{"Attribute.1", "Period"}}) in #"Renamed Columns"Looks complex but it was all done with the mouse + interface.
See if you can apply it your data. I won't be around for a few hours but I encourage you to work it out and post back later if you're struggling.
There's lots of forum examples of unpivotting pairs. Let me know how it goes
Well, it looks like your real data is a more complex situation.
Here are my suggestions:
1. Using the first 6 columns from the sample data you gave, apply the code I provided. See if you can make it work. Look at each step to see what's going on. Understand the strategy (basically reshaping the data by selecting 2 fixed columns and unpivotting the pairs) and see how each step gets you there.
Reshaping the data in Power Query allows you to start to get data that powerbi likes i.e. columnar and from there you can start to investigate star schemas.
2. Close and Apply Power Query. See if you can create a matrix visual with the reshaped data. If that works, go to 3.
----------------------
3. With your real data, start a prototype by getting rid of 90% of the columns. Make it simple. If the data is like the sample you provided, you're on a good start. If you now realise that you've asked the wrong question then repost with new sample data and requirements (you'll be closer to a solution and you've learnt a lot)
4. I don't think that sqlbi article is a good place for you to start - it's an advanced technique and solves a specific issue
5. It's highly unlikely that you're going to find a web page with "The solution" - try to understand your data and what your desired outcomes are.
Good luck.
Hey!
It took me a while for me to follow your steps but I managed to get the desired result I wanted. I reduced the number of categories to 2 and it really helped.
Really appreciate your effort and time to help me out! Cheers Mate 🙂