Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

DAX: Sumifs

Hi All..I need helped to write a dax statement for my dataset sample below

MarketPeriodSales
AUSTRALIA201801100
CANADA201801100
CZECH REPUBLIC201801100
FRANCE201801100
GREAT BRITAIN201801100
INDIA201801100
POLAND201801100
USA201801100
AUSTRALIA201802100
CANADA201802100
CZECH REPUBLIC201802100
FRANCE201802100
GREAT BRITAIN201802100
INDIA201802100
POLAND201802100

 

My data has 3 fields: Country, Time Period(yyyymm) and Sales. I need to create two columns 6MMT Sales and YTD Sales.

6MMT Sales would be sales of 6 prior months: Ex: Australia 201901 sales would be sum of sales from 201807 to 201812.

The two constraints here are Country and Time Period.

6MMT Sales would be sales of 6 prior months: Ex: Australia 201901 sales would be sum of sales from 201807 to 201812.

 

YTD Sales (201904) is sum of sales (201812 to 201903).

 

Please can some one guide me with this.

 

3 Replies

  • You can create date field using YYYYMM

     Date = date(left([period],4),right([period],2),1)

     

    Now use a date calendar and time intelligence

    Rolling 6 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-6,MONTH)) 
    Rolling 6 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date Filer],MAX(Sales[Sales Date]),-6,MONTH))  
    
    
    YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date]),"12/31")) //change end date of year
    This Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD((ENDOFYEAR('Date'[Date])),"12/31")) //change end date of year
    
    Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31")) //change end date of year
    Last YTD complete Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31")) //change end date of year
    Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31")) //change end date of year
    
    Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
    https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
    https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
    https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

    • Anonymous's avatar
      Anonymous
      Not applicable

      Amit thanks for your help. However, either I haven't implemented it properly or there is something wrong here. I had a date field in the sales table and in the calendar table I have day level data (YYYYMMDD). So when I used the DAX for each row I am getting the sales for the same month. Am I missing something here?

  • Anonymous's avatar
    Anonymous
    Not applicable

    My solution would ideally have two constraints: Country and Time Period

    Time period in case of 6MMT rolling (six prior months) and YTD (Beginning last year march and till one month prior).I can use excel to do it but I want to do it in DAX to make sure my dashboard is automated.