M code and DAX running total

dicken

Active Member
Joined
Feb 12, 2022
Messages
280
Office Version
  1. 365
Platform
  1. Windows
Hello,
pretty new to power query and dax, I've used < List.Sum list.firstN / Range and List.Accumulate to create running totals, and an index column in Dax power pivot
but I'd like to create one based on a condition, so at simples two columns 'Product ' a b c and 'sales' and create a running total of sales based on the product ;
so in Excel < sumif ( product range, 1st cell of product range (ie ($A$1:A1),range to sum >
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    AddIndex = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    RunningTotal = Table.AddColumn(AddIndex, "Running Total", each List.Sum(Table.SelectRows(Table.FirstN(Source,[Index]), (x)=> x[Product]=_[Product])[Sales])),
    Result = Table.RemoveColumns(RunningTotal,{"Index"})
in
    Result

Book1
ABCDEFG
1ProductSalesProductSalesRunning Total
2A15A1515
3B11B1111
4A12A1227
5B6B617
6B18B1835
7B13B1348
8C1C11
9A20A2047
10C14C1415
11B9B957
12A21A2168
13A5A573
14C8C823
15A7A780
16A3A383
17B17B1774
18A10A1093
19C16C1639
20C19C1958
21B4B478
22A2A295
23
Sheet1
 
Upvote 0
Solution
Hi JGordon,
not had a chance to try it but reading through it look very promising, I'm in middle of something else but will try tomorrow and let you know.

Rd
 
Upvote 0
Hi,
Gordon,
Just tried it out and works fine, now the hard part , understanding all the syntax, any help espeically regarding the use of the variable, which I'm pretty shaky on, would be gratefully received.

Richard.
 
Upvote 0
Both Table.AddColumn and Table.SelectRows have a function parameter that take the current row (as record) of their first parameter table and assigns it to its function variable.

a function is defined by wrapping a variable in parentheses followed by the "gets" operator =>

so (x)=> 2*x is a simple doubling function using the variable x, this would throw an error if used in Table.SelectColumns because a record multiplied by two doesn't compute.

But (x)=> (x[Product] = "A") would not throw an error because if would return true if the product in the current row is "A" and false otherwise.

a shorthand for function definitions is the each keyword that always uses the underscore as its variable name, so

each 2*_ is the same as (_)=> 2*_

Since I am using nested functions that have a function parameter I can't use each keyword in both because the single variable _ will only apply to one of the functions.

So I used (x)=> in one so I could refer its row reference x and compare it to the prior function's row reference _
 
Upvote 0
thanks, especially re (_) which I wasn't sure about, still lots to leaarn.

RD
 
Upvote 0
This way of writing the RunningTotal step may be more self explanatory:

RunningTotal = Table.AddColumn(AddIndex, "Running Total", (TableAddColumnRow)=>
List.Sum(Table.SelectRows(Table.FirstN(Source,[Index]), (x)=> x[Product]=_[Product])[Sales])),
 
Upvote 0
This way of writing the RunningTotal step may be more self explanatory:

RunningTotal = Table.AddColumn(AddIndex, "Running Total", (TableAddColumnRow)=>
List.Sum(Table.SelectRows(Table.FirstN(Source,[Index]), (x)=> x[Product]=_[Product])[Sales])),
Thanks once again, I think I shall be investing in DAX book before too long.
Richard.
 
Upvote 0
This way of writing the RunningTotal step may be more self explanatory:

RunningTotal = Table.AddColumn(AddIndex, "Running Total", (TableAddColumnRow)=>
List.Sum(Table.SelectRows(Table.FirstN(Source,[Index]), (x)=> x[Product]=_[Product])[Sales])),
Could you elaborate on the step < (x)=> x[Criteria] = _ [Criteria])[Sales]) > if x represents criteria , what is x[Criteria] doing ? Can I take it that _ indicates all rows of the table. Any recommendations re things i should read would be welcome.

I have to say the closest I've every got to coding is a visual basic course over 30 years ago.

Richard
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top