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

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.

Reply
Tan_LC
Helper I
Helper I

DAX Formula to count every 10,000 and also sum up to every 12 months

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 EColumn F
MonthProduct No.QtyCumulative QtyTest Identification [Desired Outcome]Month with Production [Desired Outcome]
Jan-21A01010001000 1
Feb-21A0109801980 2
Mar-21A010 1980 2
Apr-21A0105002480 3
May-21A0104502930 4
Jun-21A01067849714 5
Jul-21A0107501046410K Test6
Aug-21A010 10464 6
Sep-21A01060011064 7
Oct-21A010 11064 7
Nov-21A010 11064 7
Dec-21A010 11064 7
Jan-22A01086011924 8
Feb-22A01045312377 9
Mar-22A010345715834 10
Apr-22A010 15834 10
May-22A010 15834 10
Jun-22A010 15834 10
Jul-22A010 15834 10
Aug-22A010 15834 10
Sep-22A01057572159110K Test11
Oct-22A010350825099 12
Nov-22A010192027019 1
Dec-22A01077127790 2
Jan-21C050 0  
Feb-21C050 0  
Mar-21C050 0  
Apr-21C05034683468 1
May-21C0503543822 2
Jun-21C050 3822 2
Jul-21C050 3822 2
Aug-21C0508024624 3
Sep-21C050 4624 3
Oct-21C050 4624 3
Nov-21C0509925616 4
Dec-21C050 5616 4
Jan-22C05077871340310K Test5
Feb-22C050 13403 5
Mar-22C05023713640 6
Apr-22C050 13640 6
May-22C05069714337 7
Jun-22C05067562109310K Test8
Jul-22C05065321746 9
Aug-22C05071522461 10
Sep-22C050 22461 10
Oct-22C050 22461 10
Nov-22C050 22461 10
Dec-22C05095623417 11
Jan-21Z009 0  
Feb-21Z00912701270 1
Mar-21Z00936484918 2
Apr-21Z0098195737 3
May-21Z00952601099710K Test4
Jun-21Z009 10997 4
Jul-21Z00968511682 5
Aug-21Z009 11682 5
Sep-21Z00973212414 6
Oct-21Z009 12414 6
Nov-21Z00987013284 7
Dec-21Z00954613830 8
Jan-22Z009 13830 8
Feb-22Z00965014480 9
Mar-22Z00978115261 10
Apr-22Z009 15261 10
May-22Z00912015381 11
Jun-22Z009 15381 11
Jul-22Z00911015491 12
Aug-22Z00911915610 1
Sep-22Z00918015790 2
Oct-22Z009 15790 2
Nov-22Z00985916649 3
Dec-22Z00972017369 4

 

Please help. Thanks.

1 ACCEPTED 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"

  

View solution in original post

5 REPLIES 5
daXtreme
Solution Sage
Solution Sage

@Tan_LC 

 

Why would you want to do it in DAX? Why not where such calculations belong - in Power Query?

@daXtreme , kindly advice how to do it in Power Query.

 

Thanks.

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.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors