+ Reply to Thread
Results 1 to 8 of 8

Running totals column in power pivot data model

  1. #1
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    610

    Running totals column in power pivot data model

    Hi,

    I've been trying to see if CALCULATE can be used for a power pivot data model running total,
    I've used SUMX;

    < SUMX(FILTER(RTTest,RTTest[Index]<=EARLIER(RTTest[Index])),RTTest[Unit]) >


    but have been trying unsuccessfully to see if wrapping a sum function inside calculate can achieve the same result;

    This does not work.

    < CALCULATE(SUM(Table1[Unit]),Table1[Index]<=EARLIER(Table1[Index])) >,

    Can anyone tell me if this is possible or am I wasting my time.


    RD

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Running totals column in power pivot data model

    You can create a MEASURE to evaluate a cumulative total in [Index]:

    Please Login or Register  to view this content.
    You were nearly there - you just needed to remove the row / evaluation context before the comparative transition.

    Also - using a variable is generally simpler then using the EARLIER function.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    610

    Re: Running totals column in power pivot data model

    Hi Olly,
    I've never used varp I don't seem to be getting it right? I've attached a (very) simple workbook, with the sumx function and what I think you meant
    regarding sum and calculate.
    but am getting wrong.
    RD
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Running totals column in power pivot data model

    You don't need SUMX - this is an iterator, and in this case, you're doing a simple aggregation of a single column. You do need a context transition, which is why we use the CALCULATE function.

    Why are you trying to add this as a Calculated Column? If you do this, you lose the ability to perform your cumulative total against slices of your data, and it makes aggregation harder, not easier. Unless you're planning to group by cumulative total values, then it makes sense to add this as a MEASURE, not as a CALCULATED COLUMN.

    In your sample workbook, the measure would read:

    Please Login or Register  to view this content.

    (My apologies - the version of Excel I am using in the office today is without PowerPivot, so I can't quite see what you're trying in your calculated columns. But my advice to use a measure stands.)

  5. #5
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    610

    Re: Running totals column in power pivot data model

    Hi Olly,
    Yep dropped this measure into a pivot table and works fine, for me the only thing is I don't really understand the variable ,
    if you can recommend any sites that give a good introduction I'd be grateful. Also I prefer to see what I'm getting in a calculated column, in this case running total, in your initial reply what do you mean by row / evaluation context, which I take to mean 'Calculate' , which would just give an error?

    Thanks for the help so far, and I'll be looking into VAR MAX All, none of which I've used before.

    Richard.

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Running totals column in power pivot data model

    Variables are really useful for improving performance (calculate once, use several times), and for making context transitions easier (in this case, the variable is calculated before the context transition, so the EARLIER transition function is not needed). They also help keep your DAX code simpler and easier to trace.

    "I prefer to see what I'm getting in a calculated column" - ah, the typical refrain of the Excel user! There are several reasons why calculated columns are not always the best option. A brief example: in this case, imagine your data has an additional column - let's call it Colour. Now, if you use a calculated column for your cumulative total, you can no longer split that cumulative total by colour value. Creating a measure allows you to evaluate the formula within the context of the report - so if you have grouped your report by Colour, each cumulative colour value will be calculated appropriately and correctly, without needing to set up multiple calculated columns in advance. There are also performance benefits - calculated columns need to be evaluated for every row of your table, every time it is refreshed. Measures only calculate as needed within the report. Measures are immensely powerful.

    Here's someone much smarter than me, explaining it much better: https://exceleratorbi.com.au/calcula...-measures-dax/

    Finally, "Context". There are three types on context to understand in DAX (Row context, Filter context and Evaluation context), and it's worth taking the time to learn the theory, and understand context transition. Here's a reasonable starting point: https://adatis.co.uk/Understanding-D...ation-Context/

    Last bit of advice from me: Read sqlbi.com, and order their books. It's gold.

  7. #7
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    610

    Re: Running totals column in power pivot data model

    Oh I do appreciate what measures can do, I remember the first time creating a measure for "sum product" using sumx which when dropped into a pivot table still 'understood' the row / filter context as categories were added. I'll certainly have a look at all you recommended.

    Richard.

  8. #8
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    610

    Re: Running totals column in power pivot data model

    Hi
    Just a quick question, I've tried using the formula above but without the 'All' function and it seems to work fine, is this just good practice in case there are filters or is there another reason.
    Richard.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. power Pivot data model question
    By Poconosms1 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 03-08-2021, 10:09 AM
  2. Power Pivot. My model will not update when I add column from my database
    By Reykjavik in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-05-2021, 03:13 AM
  3. Replies: 3
    Last Post: 07-12-2019, 06:17 AM
  4. Power Pivot Data Model Error
    By MarkZang in forum Excel General
    Replies: 0
    Last Post: 01-31-2018, 11:51 AM
  5. Creating a Pivot Table from Power Pivot Data Model?
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2017, 12:02 PM
  6. [SOLVED] Power Pivot and Data Model theory
    By BDD2015 in forum Excel General
    Replies: 9
    Last Post: 11-04-2016, 09:45 PM
  7. power pivot table - Data model
    By pccamara in forum Excel General
    Replies: 2
    Last Post: 04-18-2016, 11:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1