Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Sam_BI_Analyst
Frequent Visitor

Need Help with Dax

Hello Community,

 

I need some help with correcting my dax formula. My ending_hc is working as expected but my starting_hc isn't. Please find below table with the details.

 

Ending_HC =
CALCULATE(
COUNTROWS('Headcount Report'),FILTER('Headcount Report',
'Headcount Report'[YEAR ] = SELECTEDVALUE('Headcount Report'[YEAR ]) &&
'Headcount Report'[MONTH#] = SELECTEDVALUE('Headcount Report'[MONTH#])
))

 

Starting HC =
VAR _CurrentYear = SELECTEDVALUE('Headcount Report'[YEAR ])
VAR _CurrentMonth = SELECTEDVALUE('Headcount Report'[MONTH#])
VAR _PreviousMonth = IF(_CurrentMonth = 1, 12, _CurrentMonth-1)
VAR _PreviousYear = IF(_CurrentMonth = 1, _CurrentYear-1, _CurrentYear)
var edhc = CALCULATE(
COUNTROWS('Headcount Report'),FILTER('Headcount Report',
'Headcount Report'[YEAR ] = _PreviousYear &&
'Headcount Report'[MONTH#] = _PreviousMonth
))
RETURN
IF(_CurrentYear=2021 && _CurrentMonth=2,[Starting_HC],edhc)

My output - 

 

Sam_BI_Analyst_0-1722977676418.png

 

@Ritaf1983 

Warm Regards

1 ACCEPTED SOLUTION
Sam_BI_Analyst
Frequent Visitor

I found the answer,

 

Starting_HC =
VAR CurrentYear = SELECTEDVALUE('Headcount Report'[YEAR ])
VAR CurrentMonth = SELECTEDVALUE('Headcount Report'[MONTH#])
VAR PrevYear = SELECTEDVALUE('Headcount Report'[prevYear])
VAR PrevMonth = SELECTEDVALUE('Headcount Report'[PreviousMonth])

RETURN
IF (
    CurrentYear = 2021 && CurrentMonth = 2,
    [Ending_HC],
    CALCULATE(
        COUNTROWS('Headcount Report'),
        FILTER(
            ALL('Headcount Report'),
            'Headcount Report'[YEAR ] = PrevYear &&
            'Headcount Report'[MONTH#] = PrevMonth
        )
    )
)

View solution in original post

3 REPLIES 3
Sam_BI_Analyst
Frequent Visitor

I found the answer,

 

Starting_HC =
VAR CurrentYear = SELECTEDVALUE('Headcount Report'[YEAR ])
VAR CurrentMonth = SELECTEDVALUE('Headcount Report'[MONTH#])
VAR PrevYear = SELECTEDVALUE('Headcount Report'[prevYear])
VAR PrevMonth = SELECTEDVALUE('Headcount Report'[PreviousMonth])

RETURN
IF (
    CurrentYear = 2021 && CurrentMonth = 2,
    [Ending_HC],
    CALCULATE(
        COUNTROWS('Headcount Report'),
        FILTER(
            ALL('Headcount Report'),
            'Headcount Report'[YEAR ] = PrevYear &&
            'Headcount Report'[MONTH#] = PrevMonth
        )
    )
)
Shravan133
Super User
Super User

try this refined measure and apply your logic to it:

Starting_HC =

VAR _CurrentYear = SELECTEDVALUE('Headcount Report'[YEAR ])

VAR _CurrentMonth = SELECTEDVALUE('Headcount Report'[MONTH#])

VAR _PreviousMonth = IF(_CurrentMonth = 1, 12, _CurrentMonth - 1)

VAR _PreviousYear = IF(_CurrentMonth = 1, _CurrentYear - 1, _CurrentYear)

VAR _StartingHC =

    CALCULATE(

        COUNTROWS('Headcount Report'),

        FILTER(

            'Headcount Report',

            'Headcount Report'[YEAR ] = _PreviousYear &&

            'Headcount Report'[MONTH#] = _PreviousMonth

        )

    )

RETURN

IF(

    ISBLANK(_StartingHC),

    0,

    _StartingHC

)

Hello @Shravan133,

 

I don't want to replace my values with 0.

 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors