Forum Discussion

Hyuna_8000's avatar
Hyuna_8000
Helper I
1 year ago
Solved

Using Countrows based on multiple conditions

Hi,

 

I have a dataset with the following following columns, basically I just wanted to have a calculated column, that populates the number of actions when the Action is "Promotion" or "Secondment" or both for each employee. And for employee with no Separation Date, I want the count of the number of Promotion or Secondment or both when the Effective date is between today and 12 months ago. For employee with a Separation Date, the count should be when the Effective date equals or is greater than 12 months before the Separation Date and is less than the Separation Date.

Please see this dataset, the "No. of Promotion and Secondment last 12 months" is the calculated column I want:

Row

 

Employee NameEffective DateActionSeparation DateNo. of Promotion and Secondment last 12 months

1

Jane Jones8/02/2025Hire 0
2Carrie Bradshaw1/01/2024Secondment 2
3Carrie Bradshaw6/06/2024Promotion 2
4Carrie Bradshaw26/10/2021Secondment 2
5Rebecca Lynn16/10/2022Hire 1
6Rebecca Lynn14/10/2023Secondment 1
7Yao Ming7/10/2022Secondment7/06/20231
8Zoe Meng7/01/2024Add Worker 0
9Hoi Lou5/10/2021Hire2/02/20231
10Hoi Lou5/11/2021Secondment2/02/20231
11Hoi Lou6/06/2022Promotion2/02/20231

 

I did try this Dax code for the calculated column, but it only returns either 1 for row with Promotion or Secondment with the effective date and separation date condition. For example, for Carrie Bradshaw, it only returns 1 for the Row 2 and 3, and 0 for Row 4, but what I want is 2 for Row 2, 3 and 4.

No. of Promotion and Secondment last 12 months =
VAR CurrentEmployee = 'Data'[Employee Name]
VAR SeparationDate = 'Data'[Separation Date]
VAR EffectiveStartDate = 'Data'[Effective Date]
VAR Last12MonthsStartDate = TODAY() - 365
RETURN
IF(
ISBLANK(SeparationDate),
CALCULATE(
COUNTROWS('Data'),
'Data'[Employee Name] = CurrentEmployee,
('Data'[Action] = "Promotion" && 'Employment Action History'[Action] = "Secondment"),
'Data'[Effective Start Date] >= Last12MonthsStartDate
),
CALCULATE(
COUNTROWS('Data'),
'Data'[Employee Name] = CurrentEmployee,
('Data'[Action] = "Promotion" && 'Data'[Action] = "Secondment"),
'Data'[Effective Date] >= SeparationDate - 365,
'Data'[Effective Date] <= SeparationDate
)
)

Thanks in advance, much appreciated!

  • Hyuna_8000 , Yes you can try using SUMX

     

    No. of Promotion and Secondment last 12 months =
    VAR CurrentEmployee = 'Data'[Employee Name]
    VAR SeparationDate = 'Data'[Separation Date]
    VAR Last12MonthsStartDate = TODAY() - 365

    RETURN
    IF(
    ISBLANK(SeparationDate),
    CALCULATE(
    SUMX(
    FILTER(
    'Data',
    'Data'[Employee Name] = CurrentEmployee &&
    'Data'[Action] IN {"Promotion", "Secondment"} &&
    'Data'[Effective Date] >= Last12MonthsStartDate
    ),
    1
    )
    ),
    CALCULATE(
    SUMX(
    FILTER(
    'Data',
    'Data'[Employee Name] = CurrentEmployee &&
    'Data'[Action] IN {"Promotion", "Secondment"} &&
    'Data'[Effective Date] >= SeparationDate - 365 &&
    'Data'[Effective Date] < SeparationDate
    ),
    1
    )
    )
    )

3 Replies

  • Hyuna_8000 , Try below DAX mentioned:-

     

    No. of Promotion and Secondment last 12 months =
    VAR CurrentEmployee = 'Data'[Employee Name]
    VAR SeparationDate = 'Data'[Separation Date]
    VAR EffectiveStartDate = 'Data'[Effective Date]
    VAR Last12MonthsStartDate = TODAY() - 365

    RETURN
    IF(
    ISBLANK(SeparationDate),
    CALCULATE(
    COUNTROWS('Data'),
    'Data'[Employee Name] = CurrentEmployee,
    'Data'[Action] IN {"Promotion", "Secondment"},
    'Data'[Effective Date] >= Last12MonthsStartDate
    ),
    CALCULATE(
    COUNTROWS('Data'),
    'Data'[Employee Name] = CurrentEmployee,
    'Data'[Action] IN {"Promotion", "Secondment"},
    'Data'[Effective Date] >= SeparationDate - 365,
    'Data'[Effective Date] < SeparationDate
    )
    )

    • Hyuna_8000's avatar
      Hyuna_8000
      Helper I

      Hi bhanu_gautam 
      Thanks for your prompt reply, really appreciated it! 
      It still returns 1 for individual row that matches the criteria, instead of summing up or count the total rows that match the criteria. I wonder if I should use Sumx instead, so far no luck yet.

      • bhanu_gautam's avatar
        bhanu_gautam
        Super User

        Hyuna_8000 , Yes you can try using SUMX

         

        No. of Promotion and Secondment last 12 months =
        VAR CurrentEmployee = 'Data'[Employee Name]
        VAR SeparationDate = 'Data'[Separation Date]
        VAR Last12MonthsStartDate = TODAY() - 365

        RETURN
        IF(
        ISBLANK(SeparationDate),
        CALCULATE(
        SUMX(
        FILTER(
        'Data',
        'Data'[Employee Name] = CurrentEmployee &&
        'Data'[Action] IN {"Promotion", "Secondment"} &&
        'Data'[Effective Date] >= Last12MonthsStartDate
        ),
        1
        )
        ),
        CALCULATE(
        SUMX(
        FILTER(
        'Data',
        'Data'[Employee Name] = CurrentEmployee &&
        'Data'[Action] IN {"Promotion", "Secondment"} &&
        'Data'[Effective Date] >= SeparationDate - 365 &&
        'Data'[Effective Date] < SeparationDate
        ),
        1
        )
        )
        )