Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi, in the below table, there are 3 different Product No. in Column B and the date for each of the Product range from Jan-21 till Dec-22 as show in Column A.
I would like to create a formula whereby,
1. When the Cumulative Qty (according to Product) reach every 10,000, it will show "10K Test" as in Column E.
2. If there is Qty value available in Column C, it will auto +1 in Column F (to indicate there is production in the said month), else it will remain same value as previous month. Value in Column F shall accumulate until maximum of 12 only (according to Product) and it will then recalculate starts from 1 again.
Column A | Column B | Column C | Column D | Column E | Column F |
Month | Product No. | Qty | Cumulative Qty | Test Identification [Desired Outcome] | Month with Production [Desired Outcome] |
Jan-21 | A010 | 1000 | 1000 | 1 | |
Feb-21 | A010 | 980 | 1980 | 2 | |
Mar-21 | A010 | 1980 | 2 | ||
Apr-21 | A010 | 500 | 2480 | 3 | |
May-21 | A010 | 450 | 2930 | 4 | |
Jun-21 | A010 | 6784 | 9714 | 5 | |
Jul-21 | A010 | 750 | 10464 | 10K Test | 6 |
Aug-21 | A010 | 10464 | 6 | ||
Sep-21 | A010 | 600 | 11064 | 7 | |
Oct-21 | A010 | 11064 | 7 | ||
Nov-21 | A010 | 11064 | 7 | ||
Dec-21 | A010 | 11064 | 7 | ||
Jan-22 | A010 | 860 | 11924 | 8 | |
Feb-22 | A010 | 453 | 12377 | 9 | |
Mar-22 | A010 | 3457 | 15834 | 10 | |
Apr-22 | A010 | 15834 | 10 | ||
May-22 | A010 | 15834 | 10 | ||
Jun-22 | A010 | 15834 | 10 | ||
Jul-22 | A010 | 15834 | 10 | ||
Aug-22 | A010 | 15834 | 10 | ||
Sep-22 | A010 | 5757 | 21591 | 10K Test | 11 |
Oct-22 | A010 | 3508 | 25099 | 12 | |
Nov-22 | A010 | 1920 | 27019 | 1 | |
Dec-22 | A010 | 771 | 27790 | 2 | |
Jan-21 | C050 | 0 | |||
Feb-21 | C050 | 0 | |||
Mar-21 | C050 | 0 | |||
Apr-21 | C050 | 3468 | 3468 | 1 | |
May-21 | C050 | 354 | 3822 | 2 | |
Jun-21 | C050 | 3822 | 2 | ||
Jul-21 | C050 | 3822 | 2 | ||
Aug-21 | C050 | 802 | 4624 | 3 | |
Sep-21 | C050 | 4624 | 3 | ||
Oct-21 | C050 | 4624 | 3 | ||
Nov-21 | C050 | 992 | 5616 | 4 | |
Dec-21 | C050 | 5616 | 4 | ||
Jan-22 | C050 | 7787 | 13403 | 10K Test | 5 |
Feb-22 | C050 | 13403 | 5 | ||
Mar-22 | C050 | 237 | 13640 | 6 | |
Apr-22 | C050 | 13640 | 6 | ||
May-22 | C050 | 697 | 14337 | 7 | |
Jun-22 | C050 | 6756 | 21093 | 10K Test | 8 |
Jul-22 | C050 | 653 | 21746 | 9 | |
Aug-22 | C050 | 715 | 22461 | 10 | |
Sep-22 | C050 | 22461 | 10 | ||
Oct-22 | C050 | 22461 | 10 | ||
Nov-22 | C050 | 22461 | 10 | ||
Dec-22 | C050 | 956 | 23417 | 11 | |
Jan-21 | Z009 | 0 | |||
Feb-21 | Z009 | 1270 | 1270 | 1 | |
Mar-21 | Z009 | 3648 | 4918 | 2 | |
Apr-21 | Z009 | 819 | 5737 | 3 | |
May-21 | Z009 | 5260 | 10997 | 10K Test | 4 |
Jun-21 | Z009 | 10997 | 4 | ||
Jul-21 | Z009 | 685 | 11682 | 5 | |
Aug-21 | Z009 | 11682 | 5 | ||
Sep-21 | Z009 | 732 | 12414 | 6 | |
Oct-21 | Z009 | 12414 | 6 | ||
Nov-21 | Z009 | 870 | 13284 | 7 | |
Dec-21 | Z009 | 546 | 13830 | 8 | |
Jan-22 | Z009 | 13830 | 8 | ||
Feb-22 | Z009 | 650 | 14480 | 9 | |
Mar-22 | Z009 | 781 | 15261 | 10 | |
Apr-22 | Z009 | 15261 | 10 | ||
May-22 | Z009 | 120 | 15381 | 11 | |
Jun-22 | Z009 | 15381 | 11 | ||
Jul-22 | Z009 | 110 | 15491 | 12 | |
Aug-22 | Z009 | 119 | 15610 | 1 | |
Sep-22 | Z009 | 180 | 15790 | 2 | |
Oct-22 | Z009 | 15790 | 2 | ||
Nov-22 | Z009 | 859 | 16649 | 3 | |
Dec-22 | Z009 | 720 | 17369 | 4 |
Please help. Thanks.
Solved! Go to Solution.
Hi there. Here's the M code that does exactly what you want. Please copy these queries and paste them right into the pane on the left (Queries). There are 2 functions in here, fnCumulativeSum and fnAddIndex and the main query/table, Products.
// fnCumulativeSum
(MyTable as table) as table =>
let
Source = Table.Buffer(MyTable),
TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)),
Cumulative = List.Skip(List.Accumulate(Source[Qty],{0},
(cumulative, cost) => cumulative & {List.Last(cumulative) + (if cost = null then 0 else cost)})
),
AddedRunningSum = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
in
AddedRunningSum
// fnAddIndex
(MyTable as table, StartIndex as number) as table =>
let
Source = Table.Buffer(MyTable),
AddIndex = Table.AddIndexColumn(Source, "Index", StartIndex, 1, Int64.Type)
in
AddIndex
// Products
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZbPb1MxDID/larnIcVJnB/HCcRhCDjAiWqHMVW7VDCNFYn/nhcnduKXtuulqdrvOYmdL3673fbu4dc7C9ub7a0BswxgzDBsytft/c1u+3H/U3E5EVaHglnCPj+8KGxzCrp91hDSXNYL5lqsfwrzSFh2jHnC7o56ByEmXxYYwTcOG3dQXMS6UR88jZ823/d/XkuAusjj07yTBm+E+rZ/1pPX7IERLhL39fF1jjZRX37/vYL6sH+8gqLC2k6lUFeWLXOpF3bgPLrCWRdj43Kv7MA5jwUATI4DLr9Lee1qfTNG5X0bo/Jegx2uwaiqb2NU1gHDSJu1gBn0YQHo9R2zgyaVB9DkzKFtr/FALgWhYx0NCNnLPIAxAnExa5vE4PcG+R8eN9rds4Roe5YQZxvhfEh96KsWaZnDklqXaCfDoo/zok9Ah7ch8bRByZT/fZBj7rSoQ6wJEksvQSJpg3IuE2KAwE9oS4dYEySSNijGRFI5b5w+Z6hlHYIy3G86UbVRi8vEBc/PBC2qiramxNNGhUzRvHN8QUQtKnMRAzlj8movSRvLPF08FqLnHGWtLOcIsHDWBzgj7bCdE5ioehkTTy9jYimfhrpp54Gz024I0fSHMSz6GU0bAcud0IdRsRfFLRUrDvoMrOKqzTYu0QWDUeq2arMNQ1s7xXJzRV23Vb8dNsLw2JUPigoJKRchsb6o9R2jTZTY26joLGXGg27HIvAYbaLEYE5MzbOz6Uyj5czQyQSX5A0kaYvHWSdK3OWM1PcP3197Vo2W95qo5SxlWR090XecdsbEXzlWNC+6JCBogVW8E9hBx6ttDH0W0GpxBaTP5RLU51m8Za6+WuK60Ym4an1rSrzl4iLFDMHzM06Ly2muaYkuMLcc5Pv/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"Product No." = _t, Qty = _t, #"Cumulative Qty" = _t, #"Test Identification [Desired Outcome]" = _t, #"Month with Production [Desired Outcome]" = _t]),
#"Removed Columns1" = Table.RemoveColumns(Source,{"Cumulative Qty", "Test Identification [Desired Outcome]", "Month with Production [Desired Outcome]"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns1",{{"Month", type date}, {"Qty", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Product No.", "ProdNo"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"ProdNo"}, {{"CollapsedTable", each _, type table [Month=nullable date, ProdNo=nullable text, Qty=nullable number, CumulQty=nullable number, 10KTest=nullable text, MonthWithProd=nullable number]}}),
#"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "CumulQtyTest", each fnCumulativeSum([CollapsedTable])),
#"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"CollapsedTable"}),
#"Expanded CumulQtyTest" = Table.ExpandTableColumn(#"Removed Columns", "CumulQtyTest", {"Month", "ProdNo", "Qty", "Running Sum"}, {"Month", "ProdNo.1", "Qty", "Running Sum"}),
#"Removed Columns2" = Table.RemoveColumns(#"Expanded CumulQtyTest",{"ProdNo.1"}),
#"Renamed Columns1" = Table.RenameColumns(#"Removed Columns2",{{"Running Sum", "CumulQty"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns1", "10KTest", each Number.IntegerDivide([CumulQty], 10000)),
#"Grouped Rows1" = Table.Group(#"Added Custom", {"ProdNo", "10KTest"}, {{"AllRows", each _, type table [ProdNo=nullable text, Month=date, Qty=nullable number, CumulQty=number, 10KTest=number]}, {"MinCumulQty", each List.Min([CumulQty]), type number}}),
#"Removed Columns3" = Table.RemoveColumns(#"Grouped Rows1",{"ProdNo", "10KTest"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns3", "AllRows", {"ProdNo", "Month", "Qty", "CumulQty", "10KTest"}, {"ProdNo", "Month", "Qty", "CumulQty", "10KTest"}),
#"Added Custom1" = Table.AddColumn(#"Expanded AllRows", "10KThresh", each if [MinCumulQty] = [CumulQty] and [10KTest] > 0 and [Qty] <> null then "10K Test" else ""),
#"Removed Columns4" = Table.RemoveColumns(#"Added Custom1",{"10KTest", "MinCumulQty"}),
#"Grouped Rows2" = Table.Group(#"Removed Columns4", {"ProdNo", "CumulQty"}, {{"AllRows", each _, type table [ProdNo=nullable text, Month=nullable date, Qty=nullable number, CumulQty=nullable number, 10KThresh=text]}}),
#"Grouped Rows3" = Table.Group(#"Grouped Rows2", {"ProdNo"}, {{"MinCumulOty", each List.Min([CumulQty]), type nullable number}, {"Rows", each _, type table [ProdNo=nullable text, CumulQty=nullable number, AllRows=table]}}),
#"Invoked Custom Function1" = Table.AddColumn(#"Grouped Rows3", "Index", each fnAddIndex([Rows], 1)),
#"Removed Columns5" = Table.RemoveColumns(#"Invoked Custom Function1",{"ProdNo", "Rows"}),
#"Expanded Index" = Table.ExpandTableColumn(#"Removed Columns5", "Index", {"ProdNo", "CumulQty", "AllRows", "Index"}, {"ProdNo", "CumulQty", "AllRows", "Index.1"}),
#"Added Custom2" = Table.AddColumn(#"Expanded Index", "Index.2", each if [MinCumulOty] = 0 then [Index.1] - 1 else [Index.1]),
#"Removed Columns6" = Table.RemoveColumns(#"Added Custom2",{"Index.1"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns6",0,null,Replacer.ReplaceValue,{"Index.2"}),
#"Added Custom3" = Table.AddColumn(#"Replaced Value", "MonthWithProd", each if [Index.2] <> 12 then Number.Mod([Index.2], 12) else 12),
#"Removed Columns7" = Table.RemoveColumns(#"Added Custom3",{"Index.2", "MinCumulOty", "ProdNo", "CumulQty"}),
#"Expanded AllRows1" = Table.ExpandTableColumn(#"Removed Columns7", "AllRows", {"ProdNo", "Month", "Qty", "CumulQty", "10KThresh"}, {"ProdNo", "Month", "Qty", "CumulQty", "10KThresh"})
in
#"Expanded AllRows1"
Why would you want to do it in DAX? Why not where such calculations belong - in Power Query?
Hi @Tan_LC
If the code I gave you does what you want (and it should), please tick my answer as the solution to your problem. This will help others as well when they look for an answer to their queries.
Thanks.
Hi there. Here's the M code that does exactly what you want. Please copy these queries and paste them right into the pane on the left (Queries). There are 2 functions in here, fnCumulativeSum and fnAddIndex and the main query/table, Products.
// fnCumulativeSum
(MyTable as table) as table =>
let
Source = Table.Buffer(MyTable),
TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)),
Cumulative = List.Skip(List.Accumulate(Source[Qty],{0},
(cumulative, cost) => cumulative & {List.Last(cumulative) + (if cost = null then 0 else cost)})
),
AddedRunningSum = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
in
AddedRunningSum
// fnAddIndex
(MyTable as table, StartIndex as number) as table =>
let
Source = Table.Buffer(MyTable),
AddIndex = Table.AddIndexColumn(Source, "Index", StartIndex, 1, Int64.Type)
in
AddIndex
// Products
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZbPb1MxDID/larnIcVJnB/HCcRhCDjAiWqHMVW7VDCNFYn/nhcnduKXtuulqdrvOYmdL3673fbu4dc7C9ub7a0BswxgzDBsytft/c1u+3H/U3E5EVaHglnCPj+8KGxzCrp91hDSXNYL5lqsfwrzSFh2jHnC7o56ByEmXxYYwTcOG3dQXMS6UR88jZ823/d/XkuAusjj07yTBm+E+rZ/1pPX7IERLhL39fF1jjZRX37/vYL6sH+8gqLC2k6lUFeWLXOpF3bgPLrCWRdj43Kv7MA5jwUATI4DLr9Lee1qfTNG5X0bo/Jegx2uwaiqb2NU1gHDSJu1gBn0YQHo9R2zgyaVB9DkzKFtr/FALgWhYx0NCNnLPIAxAnExa5vE4PcG+R8eN9rds4Roe5YQZxvhfEh96KsWaZnDklqXaCfDoo/zok9Ah7ch8bRByZT/fZBj7rSoQ6wJEksvQSJpg3IuE2KAwE9oS4dYEySSNijGRFI5b5w+Z6hlHYIy3G86UbVRi8vEBc/PBC2qiramxNNGhUzRvHN8QUQtKnMRAzlj8movSRvLPF08FqLnHGWtLOcIsHDWBzgj7bCdE5ioehkTTy9jYimfhrpp54Gz024I0fSHMSz6GU0bAcud0IdRsRfFLRUrDvoMrOKqzTYu0QWDUeq2arMNQ1s7xXJzRV23Vb8dNsLw2JUPigoJKRchsb6o9R2jTZTY26joLGXGg27HIvAYbaLEYE5MzbOz6Uyj5czQyQSX5A0kaYvHWSdK3OWM1PcP3197Vo2W95qo5SxlWR090XecdsbEXzlWNC+6JCBogVW8E9hBx6ttDH0W0GpxBaTP5RLU51m8Za6+WuK60Ym4an1rSrzl4iLFDMHzM06Ly2muaYkuMLcc5Pv/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"Product No." = _t, Qty = _t, #"Cumulative Qty" = _t, #"Test Identification [Desired Outcome]" = _t, #"Month with Production [Desired Outcome]" = _t]),
#"Removed Columns1" = Table.RemoveColumns(Source,{"Cumulative Qty", "Test Identification [Desired Outcome]", "Month with Production [Desired Outcome]"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns1",{{"Month", type date}, {"Qty", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Product No.", "ProdNo"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"ProdNo"}, {{"CollapsedTable", each _, type table [Month=nullable date, ProdNo=nullable text, Qty=nullable number, CumulQty=nullable number, 10KTest=nullable text, MonthWithProd=nullable number]}}),
#"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "CumulQtyTest", each fnCumulativeSum([CollapsedTable])),
#"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"CollapsedTable"}),
#"Expanded CumulQtyTest" = Table.ExpandTableColumn(#"Removed Columns", "CumulQtyTest", {"Month", "ProdNo", "Qty", "Running Sum"}, {"Month", "ProdNo.1", "Qty", "Running Sum"}),
#"Removed Columns2" = Table.RemoveColumns(#"Expanded CumulQtyTest",{"ProdNo.1"}),
#"Renamed Columns1" = Table.RenameColumns(#"Removed Columns2",{{"Running Sum", "CumulQty"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns1", "10KTest", each Number.IntegerDivide([CumulQty], 10000)),
#"Grouped Rows1" = Table.Group(#"Added Custom", {"ProdNo", "10KTest"}, {{"AllRows", each _, type table [ProdNo=nullable text, Month=date, Qty=nullable number, CumulQty=number, 10KTest=number]}, {"MinCumulQty", each List.Min([CumulQty]), type number}}),
#"Removed Columns3" = Table.RemoveColumns(#"Grouped Rows1",{"ProdNo", "10KTest"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns3", "AllRows", {"ProdNo", "Month", "Qty", "CumulQty", "10KTest"}, {"ProdNo", "Month", "Qty", "CumulQty", "10KTest"}),
#"Added Custom1" = Table.AddColumn(#"Expanded AllRows", "10KThresh", each if [MinCumulQty] = [CumulQty] and [10KTest] > 0 and [Qty] <> null then "10K Test" else ""),
#"Removed Columns4" = Table.RemoveColumns(#"Added Custom1",{"10KTest", "MinCumulQty"}),
#"Grouped Rows2" = Table.Group(#"Removed Columns4", {"ProdNo", "CumulQty"}, {{"AllRows", each _, type table [ProdNo=nullable text, Month=nullable date, Qty=nullable number, CumulQty=nullable number, 10KThresh=text]}}),
#"Grouped Rows3" = Table.Group(#"Grouped Rows2", {"ProdNo"}, {{"MinCumulOty", each List.Min([CumulQty]), type nullable number}, {"Rows", each _, type table [ProdNo=nullable text, CumulQty=nullable number, AllRows=table]}}),
#"Invoked Custom Function1" = Table.AddColumn(#"Grouped Rows3", "Index", each fnAddIndex([Rows], 1)),
#"Removed Columns5" = Table.RemoveColumns(#"Invoked Custom Function1",{"ProdNo", "Rows"}),
#"Expanded Index" = Table.ExpandTableColumn(#"Removed Columns5", "Index", {"ProdNo", "CumulQty", "AllRows", "Index"}, {"ProdNo", "CumulQty", "AllRows", "Index.1"}),
#"Added Custom2" = Table.AddColumn(#"Expanded Index", "Index.2", each if [MinCumulOty] = 0 then [Index.1] - 1 else [Index.1]),
#"Removed Columns6" = Table.RemoveColumns(#"Added Custom2",{"Index.1"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns6",0,null,Replacer.ReplaceValue,{"Index.2"}),
#"Added Custom3" = Table.AddColumn(#"Replaced Value", "MonthWithProd", each if [Index.2] <> 12 then Number.Mod([Index.2], 12) else 12),
#"Removed Columns7" = Table.RemoveColumns(#"Added Custom3",{"Index.2", "MinCumulOty", "ProdNo", "CumulQty"}),
#"Expanded AllRows1" = Table.ExpandTableColumn(#"Removed Columns7", "AllRows", {"ProdNo", "Month", "Qty", "CumulQty", "10KThresh"}, {"ProdNo", "Month", "Qty", "CumulQty", "10KThresh"})
in
#"Expanded AllRows1"
Hi there.
I'll do it for you once I get a bit of time. It's not particularly difficult to do what you want. I believe it just requires a bit of manual intervention in the M code (maybe not - need to start fiddling with it). Please bear with me.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
49 | |
26 | |
21 | |
15 | |
12 |
User | Count |
---|---|
57 | |
49 | |
44 | |
19 | |
18 |