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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
BI_Samurai
Frequent Visitor

Cumulative Measure from start of the year up until selected month

Hello,

 

I would like to create a dynamic calculation for 3 different time logic from start of the year up until the time period selected in the slicer.

 

The time intelligence is created with a switch measure in order to display dynamically:

  • Sales Single month
  • Sales Cumulative
  • Sales 12 months rolling

 

Measure Sales Time Logic on Page Test 1 returns correct values for 3 time logics, however, the goal is to show the data only until the time period selected in the year/month slicers meaning that if I select April I should see 4 bars with Jan, Feb, Mar, Apr (not one for April only nor 12 months for the whole year).

 

Test 1Test 1

 

Test 2 was created with the second calendar (Calendar 2) and Measure Sales Start of the Year Test where I get the correct time period upon the selection from the slicer, however I always get values for single month instead of dynamic time logics for cumulative and 12 months rolling. So this measure work correctly only in the scenario of single month.

 

Test 2Test 2

 

The goal is to have both correct time logic calculation and display the time period up until the month selected in the slicer.

 

Does anyone have an idea how to fix this?

 

Thank you.

 

My measures are here:

 

Sales Time Logic =

SWITCH(MIN('SWITCH TIME'[SWITCH]),

    "single month", [Sales Base],

    "cumulative", [Sales Cumulative],

    "rolling", [Sales Rolling])

 

 

Sales Start of the Year Test =

 

CALCULATE([Sales Time Logic],

DATESINPERIOD('📆Calendar'[Date],

STARTOFYEAR('📆Calendar'[Date]),

MAX('📆Calendar'[Month #]), MONTH))

 

 

Sales Cumulative =

CALCULATE(

    [Sales Base],

     DATESYTD('📆Calendar'[Date]))

 

Sales Rolling =

var var_max_date = MAX('📆Calendar'[Date])

var var_min_date = EDATE(var_max_date,-12)

 

var var_result =

CALCULATE([Sales Base],

 FILTER(ALL('📆Calendar'),

 

'📆Calendar'[Date]<= var_max_date &&

'📆Calendar'[Date]> var_min_date))

 

return

var_result

 

Sales Base =

 SUM(Sales[Total Sales])

2 REPLIES 2
BI_Samurai
Frequent Visitor

Hello @v-yangliu-msft 

thank you so much for your reply. I did a test on cumulative measure. Unfortunately when I selected April in month slicer I still see only April instead Jan-Apr time period. The flag = 1 from cumulative measure is returned only for April as well. I assume it should show in the period of Jan-Apr.  Any idea what I am doing wrong here? thanks a lot for your support.

 

cumulative =
var _selectyear=SELECTEDVALUE('📆Calendar'[Year])
var _selectmonth=SELECTEDVALUE('📆Calendar'[Month])
var _selectmonthnumber=MAXX(FILTER(ALL('📆Calendar'),'📆Calendar'[Year]=_selectyear&&'📆Calendar'[Month]=_selectmonth),'📆Calendar'[Month #])
var _column=
SELECTCOLUMNS(
    FILTER(ALL('📆Calendar'),
    '📆Calendar'[Year]=_selectyear&&'📆Calendar'[Month #]<=_selectmonthnumber),"test",'📆Calendar'[Month #])
return
IF(
    MAX(Sales[Year])=_selectyear&&MONTH(MAX(Sales[Date])) in _column,1,0)
 
Flag =
SWITCH(
    TRUE(),
 
    MAX('SWITCH TIME'[SWITCH]) = "Cumulative",[cumulative])

 

Sales Cumulative Time Period TEST 3.png

v-yangliu-msft
Community Support
Community Support

Hi  @BI_Samurai ,

 

Here are the steps you can follow:

1. Create calculated table.

Date =
var _table=
CALENDAR(  
    DATE(2023,1,1),
    DATE(2024,5,1))
return
ADDCOLUMNS(
    _table,
    "Year",YEAR([Date]),
    "Month_Number",MONTH([Date]),
    "Month_Text",FORMAT([Date],"mmm"))

vyangliumsft_0-1708485397750.png

2. Enter data – create a table.

vyangliumsft_1-1708485397752.png

3. Create measure.

Single month =
var _selectyear=SELECTEDVALUE('Date'[Year])
var _selectmonth=SELECTEDVALUE('Date'[Month_Text])
return
IF(
    MAX('Table'[Year]) = _selectyear&&MAX('Table'[Month]) =_selectmonth,1,0)
cumulative =
var _selectyear=SELECTEDVALUE('Date'[Year])
var _selectmonth=SELECTEDVALUE('Date'[Month_Text])
var _selectmonthnumber=MAXX(FILTER(ALL('Date'),'Date'[Year]=_selectyear&&'Date'[Month_Text]=_selectmonth),'Date'[Month_Number])
var _column=
SELECTCOLUMNS(
    FILTER(ALL('Date'),
    'Date'[Year]=_selectyear&&'Date'[Month_Number]<=_selectmonthnumber),"test",'Date'[Month_Number])
return
IF(
    MAX('Table'[Year])=_selectyear&&MONTH(MAX('Table'[Date])) in _column,1,0)
12 months rolling =
var _selectyear=SELECTEDVALUE('Date'[Year])
var _selectmonth=SELECTEDVALUE('Date'[Month_Text])
var _date=MAXX(FILTER(ALL('Date'),'Date'[Year]=_selectyear&&'Date'[Month_Text]=_selectmonth),[Date])
var _mindate=EOMONTH(_date,-13)
var _maxdate=EOMONTH(_date,-1)
return
IF(
    MAX('Table'[Date]) > _mindate && MAX('Table'[Date]) <= _maxdate,1,0)
Flag =
SWITCH(
    TRUE(),
    MAX('Slicer_Table'[Slicer]) = "Single month",[Single month],
    MAX('Slicer_Table'[Slicer]) = "Cumulative",[cumulative],
    MAX('Slicer_Table'[Slicer]) = "12 months rolling",[12 months rolling])

4. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_2-1708485436414.png

5. Result:

vyangliumsft_3-1708485436417.png

vyangliumsft_4-1708485512281.png

vyangliumsft_5-1708485512284.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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