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
PeteD1302
Helper I
Helper I

Calculate Set of Data for Last 3 Months for an existing measure

I have viewed numerous posts related to this topic and unable to resolve this DAX query; the query returns no data.

Last 3 Months = CALCULATE(SUMX(Table1, Table1[SLA]), DATEADD(Table1[YYYY-MM],-3,MONTH))

I have a data table, as below and want to sum the data [for SLA] for the last 3 months (I have already created a separate DATE table and linked it to YYYY-MM in table1).

I have a Measure that does not appear in the table named SLA, reported as a percentage, based on the count of projects = Yes for SLA_Ok.

Table1

ProjectTypeYYYY-MMYYYY_Qrt  SLA_Ok
120467Type4Oct-192019_Q4  Yes
118040Type4Oct-192019_Q4  No
120678OtherSep-192019_Q3  Yes
120077Type2Sep-192019_Q3  Yes
119239Type3Aug-192019_Q3  Yes
118718OtherAug-192019_Q3  No
1 ACCEPTED SOLUTION

From the help I received and a bit more searching I have been able to solve this as below;

Converting calculations to measures.
Last 3 Month Count =
CALCULATE([Count],DATESBETWEEN(table1[YYYY MM],EDATE(MIN(table1[YYYY MM]),-2),MAX(table1[YYYY MM])))

Count = COUNTROWS('table1')

Last 3 Months SLA =
CALCULATE ([SLA],DATESBETWEEN('table1'[YYYY MM], EDATE (MIN('table1'[YYYY MM]),-2), MAX('table1'[YYYY MM])))

YYYY MM in date format; e.g. Tuesday, 1 October 2019
Creating a Date table for YYYY MM (with Month and Year columns)

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

This would be my approach

  1. Ensure that the entries in YYYY-MM column are actual date entries
  2. Create a Calendar Table with the following formula: Calendar = CALENDAR(MIN(Table1[Date]),MAX(Table1[Date]))
  3. In the Calendar Table extract Year and Month with the following calculated column formulas: Year = YEAR(Calendar[Date]) and FORMAT(Calendar[Date],"mmmm")
  4. Create a relationship from the Date column of Table1 to the Date column of the Calendar Table
  5. Build 2 slicer from the Calendar Table, one each for Year and Month and select October and 2019
  6. Write this measure: Measure = CALCULATE([SLA],DATESBETWEEN(Calendar[Date],EDATE(MIN(CALENDAR[Date]),-2),MAX(Calendar[Date])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks Ashish, I have followed your advice.

I am getting a DAX syntax error as shown below

Date Table                                                  Table1

Date Table.jpgTable1.jpg

New Column DAX

DAX calc column.jpg

Hi,

You are using ]].  Use only ].


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

in case you want to use max, you need to use the filter clause. Refer example.

Sale_till_tody =CALCULATE(sum(Sales[Sales Amount]),filter(sales,Sales[Sales Date]<=maxx(Sales,Sales[Sales Date].[Date])))

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.

My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601

 

 

 

Thanks Amitchandak, I did have a look at your example DAX.

The complication I have is that the column of interest, SLA expressed as a %, is not in the table, as it is a calculated measure, so I need to use sumx instead of sum.

I tried these 2 variants; the first one showed the row count, the second one had the same value for all the display table entries.

Last 3 Months SLA =
VAR _Cuur_start = Min('Last 3 Months Date'[Last 3 Months])
VAR _Curr_END = Max('Last 3 Months Date'[Last 3 Months])
return
calculate(SUMX(table1, table1[SLA])))

 

Last 3 Months SLA2 =
CALCULATE(sumx(table1, table1[SLA]),filter(table1,table1[SLA]<=MAXX(table1,table1[YYYY-MM])))

From the help I received and a bit more searching I have been able to solve this as below;

Converting calculations to measures.
Last 3 Month Count =
CALCULATE([Count],DATESBETWEEN(table1[YYYY MM],EDATE(MIN(table1[YYYY MM]),-2),MAX(table1[YYYY MM])))

Count = COUNTROWS('table1')

Last 3 Months SLA =
CALCULATE ([SLA],DATESBETWEEN('table1'[YYYY MM], EDATE (MIN('table1'[YYYY MM]),-2), MAX('table1'[YYYY MM])))

YYYY MM in date format; e.g. Tuesday, 1 October 2019
Creating a Date table for YYYY MM (with Month and Year columns)

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.