Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Sober2010
Frequent Visitor

power query or DAX: move data when doublets/triplets in a column

Hi,

I have extracted price data via a simple sql.

Som itemID have more than one price. In my sql extract I get a new line for each ItemID, which means I get Double or triple lines with same ItemID but different prices. I don't have knowledge to correct my sql so I only get one line per ItemID

 

I hope that I may be able to do the adjustments in Power query or with DAX.

I need one line per ItemID, and the data moved as shown in the figure.

 

Does anyone have a hint ?

 

Sober2010_0-1695201668248.png

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi @Sober2010 

You can put the following code to advanced editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ0VNJRykvMTQXRhgYGENJAKVYnWsnIyAgqaQSXNMEhByRMoVLGxsZQKWNMM01MTKCSJpiSpqamUElTuKQxVjlTZLega4O5JRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, Itemname = _t, Qty = _t, Price = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", Int64.Type}, {"Itemname", type text}, {"Qty", Int64.Type}, {"Price", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Item", "Itemname"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Item", "Itemname", "Attribute"}, {{"Count", each _, type table [Item=nullable number, Itemname=nullable text, Attribute=text, Value=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Value", "Index"}, {"Value", "Index"}),
    #"Inserted Merged Column" = Table.AddColumn(#"Expanded Custom", "Merged", each Text.Combine({[Attribute], Text.From([Index], "en-US")}, " "), type text),
    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Merged Column",{"Attribute", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Merged]), "Merged", "Value")
in
    #"Pivoted Column"

Output

vxinruzhumsft_0-1695364511450.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-xinruzhu-msft
Community Support
Community Support

Hi @Sober2010 

You can put the following code to advanced editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ0VNJRykvMTQXRhgYGENJAKVYnWsnIyAgqaQSXNMEhByRMoVLGxsZQKWNMM01MTKCSJpiSpqamUElTuKQxVjlTZLega4O5JRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, Itemname = _t, Qty = _t, Price = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", Int64.Type}, {"Itemname", type text}, {"Qty", Int64.Type}, {"Price", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Item", "Itemname"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Item", "Itemname", "Attribute"}, {{"Count", each _, type table [Item=nullable number, Itemname=nullable text, Attribute=text, Value=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Value", "Index"}, {"Value", "Index"}),
    #"Inserted Merged Column" = Table.AddColumn(#"Expanded Custom", "Merged", each Text.Combine({[Attribute], Text.From([Index], "en-US")}, " "), type text),
    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Merged Column",{"Attribute", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Merged]), "Merged", "Value")
in
    #"Pivoted Column"

Output

vxinruzhumsft_0-1695364511450.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

mussaenda
Super User
Super User

Hi @Sober2010,

 

Can you please enlighten us why this step is needed?

I think it would be better if you will have a dim table for items and this table on your screenshot will be your fact table.

 

 

Hi Mussaenda. 

My thoughts was to built a table with only uniqe values in this way - so I could built relations with a fact table

I'm probably down a wrong path.  Those repeated items cause however problems for me.

I will try to split the sql extract up in several tables instead for one.

 

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors