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
Anonymous
Not applicable

Relationship influences outcome of calculated column

Hi guys,

 

I'm still struggling to understand the inner workings of DAX, and I've come across a situation that my mind cant seem to process.

 

A while ago I posted this problem:

 

The dataset is like this:

dayprogram weekSales
01/01/2017AW01400
02/01/2017AW01450
03/01/2017AW01500
04/01/2017AW01600
05/01/2017AW01450
06/01/2017AW01550
07/01/2017AW01560
08/01/2017AW02400
09/01/2017AW02450
10/01/2017AW02500
11/01/2017AW02600
12/01/2017AW02450
13/01/2017AW02550
14/01/2017AW02560

 

What I want is a fourth column (measure/column?) that gives me the cumulative sum of these values, but partitioned by program week and ordered by date.

 

The resulting table should look like this:

 

dayprogram weekSalesCumulative Weekly
01/01/2017AW01400400
02/01/2017AW01450850
03/01/2017AW015001350
04/01/2017AW016001950
05/01/2017AW014502400
06/01/2017AW015502950
07/01/2017AW015603510
08/01/2017AW02200200
09/01/2017AW02450650
10/01/2017AW025001150
11/01/2017AW026001750
12/01/2017AW024502200
13/01/2017AW025502750
14/01/2017AW025603310

 

 

The answer was this:

CumulativeWeekly =
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER (
        ALLEXCEPT ( Table1, Table1[program week] ),
        Table1[day] <= EARLIER ( Table1[day] )
    )
)

 

Which works really well. However, this stops working as soon as there is a relationship in my original data between the date column and a date table.

 

Can someone explain why this is happening? I'm not referring to that date table in my formula, why does it have such a big impact on my calculated column? Whats the solution?

 

Thanks a lot guys, I know its a long read 🙂

 

Jaap 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@Anonymous

 

Try this method:

 

Running total of Sales = CALCULATE(SUM('Running total based on category'[Sales]);FILTER(ALL('Running total based on category');'Running total based on category'[Day] <= MAX ( 'TimeDim'[Date] ));VALUES('Running total based on category'[Program week]))

 

It has the VALUES-formula, which returns the unique values from a column.

 

Let me know if this works. It should work with your time dimension. I'll also gladly elaborate, if you want me to.

View solution in original post

10 REPLIES 10

Personally I would not write this as a calculated column as the result can be calculated as a measure on demand. What do you do with this data?  What do you mean it stops working?  Are you saying the values are not returned to e calc column?  I can't see why a that would be. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
Anonymous
Not applicable

The result after creating the relationship is the following:

 

program weekdaySalesCumulativeWeekly
AW0101/01/2017400400
AW0102/01/2017450850
AW0103/01/20175001350
AW0104/01/20176001950
AW0105/01/20174502400
AW0106/01/20175502950
AW0107/01/20175603510
AW0208/01/20174003910
AW0209/01/20174504360
AW0210/01/20175004860
AW0211/01/20176005460
AW0212/01/20174505910
AW0213/01/20175506460
AW0214/01/20175607020

 

So it seems to forget about that split by program week.

 

I used this solution because it came from this other user:

https://community.powerbi.com/t5/Desktop/Sum-over-partition-by-order-by-in-DAX/m-p/215856#M95559

 

If you have a better way by using a measure, I'm all ears! 

 

(The reason I need this, is because I have weekly stock data, and daily sales data, and by subtracting the cumulative sales for that program week from the stock at the beginning of the program week, I hope to calculate daily stock levels)

 

Anonymous
Not applicable

Hi @Anonymous

 

Try this measure:

Running total of Sales = CALCULATE(SUM('Test running total'[Sales]);FILTER(ALL('Test running total'[day]); 'Test running total'[day] <= MAX ( 'Test running total'[day] ))) 

 

Running total based on category.png

Anonymous
Not applicable

Thats a great measure-based approach, but unfortunately, it still stops working as soon as I link my date table to it 😞

 

For example, if you add this table to your model:

 

Date =
ADDCOLUMNS (
CALENDAR (DATE(2000,1,1), DATE(2025,12,31)),
"DateAsInteger", FORMAT ( [Date], "YYYYMMDD" ),
"Year", YEAR ( [Date] ),
"Monthnumber", FORMAT ( [Date], "MM" ),
"YearMonthnumber", FORMAT ( [Date], "YYYY/MM" ),
"YearMonthShort", FORMAT ( [Date], "YYYY/mmm" ),
"MonthNameShort", FORMAT ( [Date], "mmm" ),
"MonthNameLong", FORMAT ( [Date], "mmmm" ),
"DayOfWeekNumber", WEEKDAY ( [Date] ),
"DayOfWeek", FORMAT ( [Date], "dddd" ),
"DayOfWeekShort", FORMAT ( [Date], "ddd" ),
"Quarter", "Q" & FORMAT ( [Date], "Q" ),
"YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q" & FORMAT ( [Date], "Q" )
)

 

And then create a relation between the date in your original table and the date in the date table, the Measure stops giving the expected result and gives you plain old cumulatives, not separated by programme week.

 

Thanks so much for thinking along!

Anonymous
Not applicable

@Anonymous

 

Try this method:

 

Running total of Sales = CALCULATE(SUM('Running total based on category'[Sales]);FILTER(ALL('Running total based on category');'Running total based on category'[Day] <= MAX ( 'TimeDim'[Date] ));VALUES('Running total based on category'[Program week]))

 

It has the VALUES-formula, which returns the unique values from a column.

 

Let me know if this works. It should work with your time dimension. I'll also gladly elaborate, if you want me to.

Anonymous
Not applicable

Hey Martin, that works perfectly! Thanks a lot!

 

The date column is the primary key in most tables in my model (its a model full of aggregates mostly, main data is too big to load). Thats why I needed it. It makes everything interact nicely.

 

Can you elaborate on why you need to filter for the unique values? What extra data is created when we make that relationship, that we then have to filter? 

Anonymous
Not applicable

@Anonymous

 

I'm glad it worked out for you. Don't forget to give me kudos Smiley Wink

 

I understand. Then, of course, you need the time dimension/date table.

 

It's not like we need to create any new relation or table. We need to have the appropriate conditions: Do this calculation, when row represents some category. When using VALUES(), it returns the unique categories, or, rather, it splits up the calculation between the unique categories. Does that make sense? It's basically like adding conditions to your calculations. example: Calculate some sum, only when category is equal to "AW01".

Anonymous
Not applicable

OK that's pretty clear, but why did creating the relationship with the date table incfluence the outcome of that measure? What changed that made it necessary to change the measure?

Anonymous
Not applicable

Oh, sorry, I misunderstood. The filtering mechanism alters, when you use the dates from the date table. I'm not sure exactly how the mechanism alters, but I know that it depends on what table the date column is located at. This is what I know about the filter mechanism: Imagine you filter one table, which is related to another table with bi-directional filtering. Then you are going to filter both tables, when chosing to filter either on category or on date.

 

So to sum up I know 2 things about it:

 

- Filtering mechanism depends on how you created your date table.

- Filtering mechanism depends on the relation, you've created between the tables.

 

Does that answer your question? I'll gladly elaborate on anything (within my knowledge). 

Anonymous
Not applicable

@Anonymous

 

I'll add the date-table and try to make it work with this. However, I must ask: Is there any good reason for using a date-table right now, seeing as you good the desired result without one?

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.