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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
LoryMenCR
Helper I
Helper I

converting table by distributing values

Dear All,

I have a table that looks like this (TABLE1):

CodeGroupValueDate
1abc100015/06/2024
1xyz150030/06/2024
2abc200010/07/2024
3xyz300015/07/2024
4abc100001/06/2024
4abc50002/09/2024
5xyz10003/09/2024

 

What I want to achieve is: for certain codes I want to distribute the value based on percentages within the month.

 

I have another table that control percentages and it looks like this (TABLE2):

Start CodeStart Code DescriptionEnd Code%
2Fruit140
2Fruit460
3Grocery120
3Grocery430
3Grocery550

 

This means that Code number 2 values will always be splitted throught the end codes (1 and 4) with certain percentages.

 

What I want to achieve through PowerQuery is converting TABLE1 as follows:

CodeGroupValueDate
1abc100015/06/2024
1xyz150030/06/2024
1FRUIT80010/07/2024
1GROCERY60015/07/2024
4abc100001/06/2024
4FRUIT120010/07/2024
4GROCERY90015/07/2024
4abc50002/09/2024
5GROCERY1500

15/07/2024

5xyz10003/09/2024

 

The transformed TABLE1 is presenting distributed values as per TABLE2 while original codes that have been distributed are no longer shown.

 

I'm struggling with this a lot 😞

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

lbendlin_0-1731491952812.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc5BDoAgDATAv/RMwtKCxrcQDuon1NeL1FDx0h4mu23OFMjRuu11BgDPSh6TZ3Ck4tSP82rQXDA49zy/+eqzufS8WP/H4/8+wtBvrufBHotxsveUxbjc", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Group = _t, Value = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type number}, {"Date", type date}},"en-GB"),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Code"}, Table2, {"Start Code"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Start Code Description", "End Code", "%"}, {"Start Code Description", "End Code", "%"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Table2",each [Code],each if [End Code]=null then [Code] else [End Code],Replacer.ReplaceValue,{"Code"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",each [Group],each if [End Code]=null then [Group] else [Start Code Description],Replacer.ReplaceValue,{"Group"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",each [Value],each if [End Code]=null then [Value] else [Value]/100*[#"%"],Replacer.ReplaceValue,{"Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value2",{{"Value", Currency.Type}, {"Code", type text}, {"Group", type text}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"Code", "Group", "Value", "Date"})
in
    #"Removed Other Columns"

 

see attached

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

lbendlin_0-1731491952812.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc5BDoAgDATAv/RMwtKCxrcQDuon1NeL1FDx0h4mu23OFMjRuu11BgDPSh6TZ3Ck4tSP82rQXDA49zy/+eqzufS8WP/H4/8+wtBvrufBHotxsveUxbjc", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Group = _t, Value = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type number}, {"Date", type date}},"en-GB"),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Code"}, Table2, {"Start Code"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Start Code Description", "End Code", "%"}, {"Start Code Description", "End Code", "%"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Table2",each [Code],each if [End Code]=null then [Code] else [End Code],Replacer.ReplaceValue,{"Code"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",each [Group],each if [End Code]=null then [Group] else [Start Code Description],Replacer.ReplaceValue,{"Group"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",each [Value],each if [End Code]=null then [Value] else [Value]/100*[#"%"],Replacer.ReplaceValue,{"Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value2",{{"Value", Currency.Type}, {"Code", type text}, {"Group", type text}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"Code", "Group", "Value", "Date"})
in
    #"Removed Other Columns"

 

see attached

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.