Forum Discussion

noobtopowerbi's avatar
noobtopowerbi
Frequent Visitor
2 years ago

compare with count previous year

I have a table1 containing customerid, month, presenceboolean. I have a calendartable containing date,month,year.

Now I want to visualize a table with columns year, distinctcount customerid and distinctcount customerid previous year

I created two measures:

for current year : this_year= calculate(distinctcount(table1[customerid),filter(presenceboolean = 1))

for previous year :

  previous_year = calculate(distinctcount(table1[customerid),sameperiodlastyear('calendartable'[date],filter(presenceboolean = 1))

 

the month field in table1 is formatted dd-mm-yyyy and is linked to the datefield in the calendertable

the dax formula for previous year does not give any result. I used the function previousyear to, but that did not give results either

 

What is wrong with my formula. Google did not help 🙂

 

8 Replies

  • ExcelMonke's avatar
    ExcelMonke
    Impactful Individual

    Hello, 
    Based off your description, try the following:

     

    Previous Year =
    VAR Previous_Y = Year(MAX(DateTable[Date]))-1
    
    Return
    CALCULATE(COUNT(Table1[CustomerID]),FILTER(ALL(DateTable),Year(DateTable[Date])=Previous_Y))
  • noobtopowerbi's avatar
    noobtopowerbi
    Frequent Visitor

    I must be doing something wrong, I get the right results for previous year but not for the actual year.
    As I cannot add a pbix file to my posts I will give as much informations as possible :

    This is the measure for current year :

    presence this year = calculate(DISTINCTCOUNT('Table'[employee]),
            Filter(all('Table'),'Table'[month]= 9),
            'Table'[presence]=1
    )
    For previous year: 
    presence previous year =
     VAR Previous_Year = max('Table'[year])-1
     Return
     calculate(DISTINCTCOUNT('Table'[employee]),

            Filter(all('Table'),'Table'[month]= 9),'Table'[year]=Previous_Year,
            Filter(all('Table'),'Table'[presence]=1))

    Now I get for the current year a count of all employees while not every value for presence =1 in month 9

     

     

     
     
    • ExcelMonke's avatar
      ExcelMonke
      Impactful Individual

      Hi there, you may have a parentheses misplaced 🙂 
      Try:

      presence this year = calculate(DISTINCTCOUNT('Table'[employee]),
              Filter(all('Table'),'Table'[month]= 9,
              'Table'[presence]=1)
      )
  • noobtopowerbi I would be very careful using this approach because it is not the best practice and will hit the performance issues on the larger data table. If you want to do the things right way, following best practices, add the date table and use that for all-time intelligence-related measures. The choice is yours. Do it right, or have a short-term solution.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for your answer. I tried the same using a calendar table containing year,month and year_month. Lin ked by year_month, but that did not help to.
      I will dive into the suggestions on date topics. Sometime I will solve this 🙂

      • ExcelMonke's avatar
        ExcelMonke
        Impactful Individual

        Just remember, it would be best practice to have your date table only contain unique values.