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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! 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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors