Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Dates mess

Hi,

I have a table with several dates columns that I need to use. But I'm not getting the results I want.

I've created a dates tabel, and created a relationship between the dates columns that I need and the dates table:

My dates columns:

So, e.g, I'm trying to count candidates added last month:

CandidatesLast30 = CALCULATE(COUNT(Assignments[CandidateId]); PREVIOUSMONTH(Dates[Date].[Date]))

But it gives me blank result.

This gives me an error:

CandidatesLast30 = CALCULATE(COUNT(Assignments[CandidateId]); PREVIOUSMONTH(Assignments[DateAddedCandidate]))

I've also tried this without results:

CandidatesLast30 = CALCULATE(COUNT(Assignments[CandidateId]); USERELATIONSHIP(Assignments[DateAddedCandidate]; Dates[Date]);DATESYTD(Dates[Date].[Date]))

Can any one point me in the direction of what I'm doing wrong?

 

12 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous ,

     

    1) Remove relations between Dates and Assignment Table

    2) Use below method to calculate measures.

    CandidatesLast30 =
    CALCULATE (
        SUMX (
            ADDCOLUMNS (
                Dates,
                "Total", CALCULATE (
                    COUNTA ( Assignments[CandidateId] ),
                    FILTER ( Assignments, Dates[Date] = Assignments[DateAddedCandidate] )
                )
            ),
            [Total]
        ),
        FILTER (
            Assignments,
            MONTH ( Assignments[DateAddedCandidate] )
                = MONTH ( TODAY () ) - 1
        )
    )

     For creating  other measures you can edit Red and Green areas as per your requirement.

     

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

      Thanks for answering.

       

      I tried removing the connection and applying your code, but it still gives me blank results?

      • Anonymous's avatar
        Anonymous
        Not applicable

        Anonymous ,

         

        Can't give solution without looking data

        1) Check Data Types of Date Columns

        2) Check whether you have used proper columns in formula.

        3) Post a snap of relationship pane and data table so that I can have a look

         

        You want count of Candidates based on DateAddedCandidate column for last month, right?

         

         

  • kcantor's avatar
    kcantor
    Community Champion

    Anonymous 

    Sometimes you need to go from the most basic calculation to determine where the failure it. I would suggest building this in blocks. It is tempting to roll all of the logic into a single calculation but in situations where something breaks it is harder to find the issue. Plus, in measure, other than appearing in the field lists, you aren't really losing performance for having them available to build agains.

    I would suggest first building a count and using it to create more defined logic.

    CandidatesLast30 = CALCULATE(COUNT(Assignments[CandidateId]); PREVIOUSMONTH(Dates[Date].[Date]))

    Candidates Added = COUNT(Assignments[CandidateID])   -- side note, in my data I would use DISTINCTCOUNT

    Drop that calculation onto a table with the months as columns and see if it works. 

    Does it work? Do you get the result you should? If yes, build a calculation for the previous month. I personally use DATEADD instead of the wrappers like PREVIOUSMONTH as I am old school.

    Candidates Added Last 30 = CALCULATE([Candidates Added], DATEADD(Dates[Date], -30, Days))

    Drop that on your table as well. Does it give you the last months total in the current month? 

    Keep in mind that the filter for month needs to be set in the table or by the page for this to work. If you have just a card, you need to define current month in order for last month to attribute correctly.

    If these measures work, you can build them into a single measure if you prefer using Var like this:

    Candidates Added Last Month =

         Var CandidatesAdded = COUNT(Assignments[CandidateID])

         Return

         CALCULATE([CandidatesAdded], DATEADD(Dates[Date], -30, Days))

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi kcantor  and thanks for contributing!

       

      When counting all the candidatesID, i get 2845, or 1532 when using distinctcount.

       

      Candidates Added = COUNT(Assignments[CandidateID]) 

      But when adding the dates, I get some issues. Days does not work for me, I get an error. I need to use day.

      Candidates Added Last 30 = CALCULATE([Candidates Added]; DATEADD(Dates[Date]; -30; DAY))

      Still, I get 2845 from that ones as well.

       

       

       

      • kcantor's avatar
        kcantor
        Community Champion

        Anonymous 

             *When counting all the candidatesID, i get 2845, or 1532 when using distinctcount.

        Which number is correct?My assumption would be that distinct would give you the correct number. 

             *Days does not work for me, I get an error. I need to use day.

        Yes, sometimes when typing in this forum I use the plural because I get in a hurry. Sorry, my bad.

         

        If you use distinct count and DAY and put the month on the columns, does the number for last 30 day give you the same number as the distinct count for the previous month? (Which makes me question if you want month over month shuold we use -1, MONTH in the calculation instead)

        Can you share any data or a file?

        Also, have you used this resource before? It may be a better method for you.

        https://powerbi.tips/2016/07/measures-month-to-month-percent-change/