running or cumulative measure - dax

I would appreciate any advises for my problem :
I have a list of stock with daily values (so several stocks and one value per day for each).
I'm trying to do a cumulative margin % on the total portfolio from the beginning of the year as a measure so i have the results on a daily basis.
So by example if the total portfolio value is 100 one day and 102 the day after and 104 the following day, i would like to have a measure with (for these 3 days) 0, 2, 4%.
I have a measure calculating the margin % of the whole portfolio per day (i can't have a column as the data is not portfolio but stock based) :
And what i would like to achieve is the following :
I tried to do a =CALCULATE(sum(dailies[marge_daily_percent_measure]); FILTER(all(dailies);INT(dailies[Date (Year)])=[annee]))
(the filter is to get the current year data) but the sum cannot be applied to the measure (he's looking for a column).
I also tried a TOTALYTD but i then have 2 issues : The Sum still cannot be applied to the measure and i also need the result on a daily basis.
Thanks for any hints.

Assuming your table with stock prices looks like this
Date
Value
30 December 2021
104
31 December 2021
106
03 January 2022
107
04 January 2022
107
05 January 2022
106
06 January 2022
95
07 January 2022
106
10 January 2022
110
I have calculated a Margin measure, DAX below. And cumulative measure using SUMX.
DAX: Margin
Margin =
VAR _SelectedDate =
SELECTEDVALUE ( 'Table'[Date] )
VAR _SelectedValue =
SELECTEDVALUE ( 'Table'[Value] )
VAR _PreviousDate =
CALCULATE ( MAX ( 'Table'[Date] ), 'Table'[Date] < _SelectedDate )
VAR _PreviousValue =
CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Date] = _PreviousDate )
VAR Margin =
DIVIDE ( _SelectedValue - _PreviousValue, _PreviousValue )
RETURN
Margin
DAX: Margin Cumulative
Cumulative Margin =
VAR _SelectedDate =
SELECTEDVALUE ( 'Table'[Date] )
VAR Cumulative =
CALCULATE (
SUMX ( VALUES ( 'Table'[Date] ), [Margin] ),
'Table'[Date] <= _SelectedDate
)
RETURN
Cumulative
Bear in mind that the final percentage value you get from Cumulative Margin is not the same as the difference from the first value against the last value. In this case, (110-104)/104 = 5.77%. With the Cumulative, I get 6.91%
Output

One way I've built cumulative measures in the past is to do the following logic to filter on date. Assuming you use this in some kind of time-sliced view (like your table, or a linechart), this should only grab the dates on/before 'todays' date for each row.
_Cumulative_ClosedTasks =
CALCULATE (
[_ClosedTasks],
FILTER (
ALL ('Date'[Date]),
'Date'[Date] <= MAX ('Date'[Date])
)
)
([_ClosedTasks] is just a basic SUM metric)
Does this approach work for your data?

Related

DAX function not giving me 100% accurate result

I hope someone can help. I am stuck.
Below are two different DAX functions to calculate my averages.
The first one (Average Coalesce function) gives me the precise results with full data e.g. Q1 & Q2 2022, but it doesn't calculate the quarters correctly as e.g Q3 this year. I do not have data for August/September.
The second Average Connector Usage is not as precise in the result but it calculates the Quarter averages correctly.
It all started with my base function to crete further formulas:
SDR ID average per CP ID =
AVERAGEX(
KEEPFILTERS(VALUES('FACT TABLE'[CP ID])),
CALCULATE(COUNTA('FACT TABLE'[SDR ID]))
)
SDR ID is my unique transaction number and CP ID is my unique location number.
I have tried many measures and these two are the closest to return correct results.
No.1.
Average COALESCE =
IF (
NOT ISEMPTY ( 'FACT TABLE' ),
AVERAGEX (
VALUES ( 'Calendar'[Date] ),
COALESCE ( [SDR ID average per CP ID], 0 )
)
)
No.2.
Average Connector Usage =
IF (
NOT ISEMPTY ( 'FACT TABLE' ),
VAR minDate =
MIN ( 'FACT TABLE'[Start Date] )
VAR maxDate =
MAX ( 'FACT TABLE'[Start Date] )
RETURN
CALCULATE (
AVERAGEX (
VALUES ( 'Calendar'[Date] ),
COALESCE ( [SDR ID average per CP ID], 0 )
),
DATESBETWEEN ( 'Calendar'[Date], minDate, maxDate )
)
)
These are the results.
Date & Connector
Average COALESCE
Average Connector Usage
2022
1.54
2.31
Q2
2.35
2.35
Rapid
2.35
2.35
AC
0.19
0.21
CCS
1.89
1.89
ChaDeMo
0.27
0.28
Q3
0.74
2.19
Rapid
0.74
2.19
AC
0.04
0.27
CCS
0.57
1.68
ChaDeMo
0.13
0.40

PowerBI line chart growth from 100%

In PowerBI, I want to compare the value growth of categories (lets take A and B) over time from any starting year. To compare this easily, I am using a line graph with the time on the x-axis and category as a legend. I would like both categories to start at 100% and show the growth relative to that starting point. I then want to be able to use a continuous date slicer to vary the start and end-points of my line graph.
I've created a dummy data to illustrate this
Category, Year, Value
A 2000 5
A 2001 8
A 2002 8
A 2003 10
B 2000 10
B 2001 8
B 2002 12
B 2003 10
Without any date filter, I would like to display years 2000-2003 with the following values for the lines:
A: 100%, 160%, 160%, 200%
B: 100%, 80%, 120%, 100%
(The first value of category A is 5. Therefore the line graph should display A's values relative to 5. It's values 5, 8, 8, 10 are then displayed as the mentioned percentages. The first value of category B is 10, so B's values should be displayed relative to 10).
With a date slicer set to filter years 2001-2003, I want the line values to become:
A: 100%, 100%, 125%
B: 100%, 150%, 125%
(Due to the slicer the first value of category A is 8, so I want the % values relative to 8. The first value of B is also 8)
I was thinking of writing a measure for this. Can anyone help me with it? Thank you in advance.
You can create a measure to establish the earliest filtered year, the value for that year, then divide each evaluated value by the min year value:
MyMeasure =
VAR MinYear =
CALCULATE (
MIN ( MyTable[Year] ),
ALLSELECTED ( MyTable[Year] )
)
VAR BaseValue =
CALCULATE (
SUM ( MyTable[Value] ),
REMOVEFILTERS ( MyTable[Year] ),
MyTable[Year] = MinYear
)
VAR CurrentValue =
SUM ( MyTable[Value] )
RETURN
DIVIDE (
CurrentValue,
BaseValue
)
Which results in:

DAX - How to sum a measure over the last N weeks, this year and last year

I have a measure [Total] reported by a custom week-ending date field 'Date'[CustWEndingDate] from Date table, based on Sat - Fri weeks (so each week ends on a Fri), plus an associated 'Date'[WeekNum] and 'Date'[Year] to that. Data looks like this:
[CustWEndingDate], [Year], [WeekNum], [Total]
3/29/2019, 2019, 13, 400
4/5/2019, 2019, 14, 350
4/12/2019, 2019, 15, 420
4/19/2019, 2019, 16, 390
...
3/27/2020, 2020, 13, 315
4/3/2020, 2020, 14, 325
4/10/2020, 2020, 15, 405
4/17/2020, 2020, 16, 375
My question is this: How do I create DAX measure to calculate last 3 weeks this year OVER same last 3 weeks last year? For example, week 14, 15 and 16 this year (325+405+375) vs same week 14, 15 and 16 last year (350+420+390)?
Thank you in advance for any help you can provide!
I would use the following approach:
Calculate scalar with today's date
Calculate scalar with year of today's date
Calculate scalar with WeekNum associated to today's date
Calculate scalar with CY value for last 3 weeks this year
Calculate scalar with PY value for (same) last 3 weeks previous year
Calculate ratio
Here is the technical implementation:
Joel's Measure:=
VAR _TODAY = TODAY()
VAR _YEAR = YEAR(_TODAY)
VAR _WEEKNUM = CALCULATE(MIN('Date'[WeekNum]), 'Date'[Date] = _TODAY)
VAR _CY = CALCULATE([MEASURE], 'Date'[Year] = _YEAR, 'Date'[WeekNum] IN {_WEEKNUM, _WEEKNUM - 1, _WEEKNUM - 2})
VAR _PY = CALCULATE([MEASURE], 'Date'[Year] = _YEAR - 1, 'Date'[WeekNum] IN {_WEEKNUM, _WEEKNUM - 1, _WEEKNUM - 2})
RETURN
DIVIDE(_CY - _PY, ABS(_PY))
This should solve your problem. If not, please share a feedback with the result.

Finding cummulative sum of MAX values

I need to calculate the cumulative sum of Max value per period (or per category). See the embedded image.
So, first, I need to find max value for each category/month per year. Then I want to calculate the cumulative SUM of these max values. I tried it by setting up max measure (which works fine for the first step - finding max per category/month for a given year) but then I fail at finding a solution to finding cumulative SUM (finding the cumulative Max is easy, but it is not what I'm looking for).
Table1
Year Month MonthlyValue MaxPerYear
2016 Jan 10 15
2016 Feb 15 15
2016 Mar 12 15
2017 Jan 22 22
2017 Feb 19 22
2017 Mar 12 22
2018 Jan 5 17
2018 Feb 16 17
2018 Mar 17 17
Desired Output
Year CumSum
2016 15
2017 37
2018 54
This is a bit similar to this question and this question and this question as far as subtotaling, but also includes a cumulative component as well.
You can do this in two steps. First, calculate a table that gives the max for each year and then use a cumulative total pattern.
CumSum =
VAR Summary =
SUMMARIZE(
ALLSELECTED(Table1),
Table1[Year],
"Max",
MAX(Table1[MonthlyValue])
)
RETURN
SUMX(
FILTER(
Summary,
Table1[Year] <= MAX(Table1[Year])
),
[Max]
)
Here's the output:
If you expand to the month level, then it looks like this:
Note that if you only need the subtotal to work leaving each row as a max (15, 22, 17, 54) rather than as a cumulative sum of maxes (15, 37, 54, 54), then you can use a simpler approach:
MaxSum =
SUMX(
VALUES( Table1[Year] ),
CALCULATE( MAX( Table1[MonthlyValue] ) )
)
This calculates the max for each year separately and then adds them together.
External References:
Subtotals and Grand Totals That Add Up “Correctly”
Cumulative Total - DAX Patterns

Sum of a Column resulting from Summarize Function in DAX

I need to sum the values of column resulting from the table resulting from Summarize Funtion.
For e.g. my Data Set 'Tab' is like this
Type Value
A 10
A 10
A 10
B 20
B 20
B 20
C 30
C 30
C 30
The result from Summarize(Tab,[Type],AVG([Value])) will be like following
A 10
B 20
C 30
And the final result required from this result set is 10+20+30 i.e. 60.
Please help
You can use SUMX function.
Sum of Avg =
SUMX (
SUMMARIZE ( Tab, [Type], "Total Average", AVERAGE ( Tab[Value] ) ),
[Total Average]
)
It will give you the total if there is not any Type context affecting the measure:
Let me know if this helps.
You need to declare a name for it.
Total Value = Summarize(Tab,'Tab'[Type],"Total value",SUM('Tab'[Value])

Resources