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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.