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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
vivek_babu
Helper II
Helper II

Countrows not working properly

Hi,

 

I am trying to count the rows based on the year and month column selected in the slicer. When a user selects Year = 2022 and Month = Jan then i need to count the rows for the previous month. In this case i need to count the rows for year = 2021 and month = Dec. I tried below logic but it is giving me blanks for all Jan. Can someone help me to fix the issue?

Screenshot:

vivek_babu_0-1731926276946.png

 

Measure = 

var r_year = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Year_windows])
var r_month = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Month_windows])
return
IF(
    r_month ="Jan",
CALCULATE(COUNTROWS(BLADE_LOGIC_FIM),
FILTER(BLADE_LOGIC_FIM,BLADE_LOGIC_FIM[Reporting Year_fim] = r_year - 1 &&
BLADE_LOGIC_FIM[Reporting Month_fim] ="Dec")),
0
Vivek N
1 ACCEPTED SOLUTION
vivek_babu
Helper II
Helper II

Hi All,

 

The issue was with the filter function inside the calculate function. I removed the filter and directly applied the condition and it worked. Thanks for your help!

Prev_Sales =
Var r_year = SELECTEDVALUE('Table'[Year])
var r_month = SELECTEDVALUE('Table'[Reporting Month Number])
VAR prev_month = IF(r_month = 1, 12, r_month - 1)
VAR prev_year = IF(r_month = 1, r_year - 1, r_year)
RETURN
CALCULATE(SUM('Table'[Sales]),
'Table'[Year] = prev_year && 'Table'[Reporting Month Number] = prev_month)
 
Regards
Vivek N

View solution in original post

8 REPLIES 8
vivek_babu
Helper II
Helper II

Hi All,

 

The issue was with the filter function inside the calculate function. I removed the filter and directly applied the condition and it worked. Thanks for your help!

Prev_Sales =
Var r_year = SELECTEDVALUE('Table'[Year])
var r_month = SELECTEDVALUE('Table'[Reporting Month Number])
VAR prev_month = IF(r_month = 1, 12, r_month - 1)
VAR prev_year = IF(r_month = 1, r_year - 1, r_year)
RETURN
CALCULATE(SUM('Table'[Sales]),
'Table'[Year] = prev_year && 'Table'[Reporting Month Number] = prev_month)
 
Regards
Vivek N
Anonymous
Not applicable

Hi @vivek_babu 

 

Glad to know that you found the cause of the problem and solved it.
Thank you for sharing your solution. Please consider accepting your reply as a solution that will benefit other users experiencing similar problem.

 

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

Hi @Anonymous ,

Yes i did! Thank You!


Regards

Vivek N

vivek_babu
Helper II
Helper II

Hi,

Please find the below sample data, Expectation is that the when user selects the year and month in the slicer then it needs to show the previous month sales. This is just an example i created to mimic my original requirement.

Current Logic:

Prev_Sales =
Var r_year = SELECTEDVALUE('Table'[Year])
var r_month = SELECTEDVALUE('Table'[Month])
VAR previous_month =
SWITCH(
r_month,
"Jan", "Dec",
"Feb", "Jan",
"Mar", "Feb",
"Apr", "Mar",
"May", "Apr",
"Jun", "May",
"Jul", "Jun",
"Aug", "Jul",
"Sep", "Aug",
"Oct", "Sep",
"Nov", "Oct",
"Dec", "Nov"
)
VAR previous_year =
IF(r_month = "Jan", r_year - 1, r_year)
RETURN
CALCULATE(SUM('Table'[Sales]),
FILTER('Table','Table'[Year] = previous_year && 'Table'[Month] = previous_month))
This is giving me blanks as result not sure why the filtering is not happening properly 

Sample Data,
YearMonthSales
2024Jan100
2024Feb200
2024Mar300
2024Apr400
2024May500
2024Jun600
2024Jul700
2024Aug800
2024Sep900
2024Oct1000
2024Nov1100
2023Jan50
2023Feb60
2023Mar70
2023Apr80
2023May90
2023Jun100
2023Jul1200
2023Aug1300
2023Sep1400
2023Oct1500
2023Nov1600
2023Dec1700
2022Jan2000
2022Feb3000
2022Mar4000
2022Apr5000
2022May6000
2022Jun7000
2022Jul8000
2022Aug9000
2022Sep10000
2022Oct11000
2022Nov12000
2022Dec13000
2021Jan20000
Regards
Vivek N
vivek_babu
Helper II
Helper II

Hi @Bibiano_Geraldo @shafiz_p @Kedar_Pande 

 

Thanks for providing your solutions. Unfortunately, all the solution is giving me blank values. In all of three solutions the previous year and previous month value is coming correctly but when we countrows of the table by filtering the table's year and month with our previous year and previous month values is not working. Not sure why it is not filtering the data properly 



@Jai-Rathinavel @lbendlin @DataNinja777 @FreemanZ @Greg_Deckler @shafiz_p @Ritaf1983 @Kedar_Pande @v-jialongy-msft @v-xianjtan-msft @vojtechsima

Regards

Vivek N

Bibiano_Geraldo
Super User
Super User

Hi @vivek_babu ,
Create a calculated column for Month number using DAX bellow, if you already have one, just skip this step:

Reporting Month Number = 
SWITCH(
    PA_TOOL_COMPLIANCE_WINDOWS[Reporting Month_windows],
    "Jan", 1,
    "Feb", 2,
    "Mar", 3,
    "Apr", 4,
    "May", 5,
    "Jun", 6,
    "Jul", 7,
    "Aug", 8,
    "Sep", 9,
    "Oct", 10,
    "Nov", 11,
    "Dec", 12
)

 

Now youu can create a measure to calculate previous month total by this DAX:

Measure = 
VAR r_year = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Year_windows])
VAR r_month = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Month Number_windows]) -- Coluna com números para meses (1 = Jan, 12 = Dec)
VAR prev_month = IF(r_month = 1, 12, r_month - 1)
VAR prev_year = IF(r_month = 1, r_year - 1, r_year)
RETURN
CALCULATE(
    COUNTROWS(BLADE_LOGIC_FIM),
    FILTER(
        BLADE_LOGIC_FIM,
        BLADE_LOGIC_FIM[Reporting Year_fim] = prev_year &&
        BLADE_LOGIC_FIM[Reporting Month Number_fim] = prev_month
    )
)
shafiz_p
Super User
Super User

Hi @vivek_babu  Try this:

Measure = 
var r_year = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Year_windows])
var r_month = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Month_windows])
var prev_date = EOMONTH(DATE(r_year, MONTH(DATEVALUE("1 " & r_month & " " & r_year)), 1), -1)
var prev_year = YEAR(prev_date)
var prev_month = FORMAT(prev_date, "MMM")
return
CALCULATE(
    COUNTROWS(BLADE_LOGIC_FIM),
    FILTER(
        BLADE_LOGIC_FIM,
        BLADE_LOGIC_FIM[Reporting Year_fim] = prev_year &&
        BLADE_LOGIC_FIM[Reporting Month_fim] = prev_month
    )
)

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

Best Regards,
Shahariar Hafiz

Kedar_Pande
Super User
Super User

@vivek_babu 

Updated Measure:
Measure =
VAR r_year = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Year_windows])
VAR r_month = SELECTEDVALUE(PA_TOOL_COMPLIANCE_WINDOWS[Reporting Month_windows])
VAR previous_month =
SWITCH(
r_month,
"Jan", "Dec",
"Feb", "Jan",
"Mar", "Feb",
"Apr", "Mar",
"May", "Apr",
"Jun", "May",
"Jul", "Jun",
"Aug", "Jul",
"Sep", "Aug",
"Oct", "Sep",
"Nov", "Oct",
"Dec", "Nov"
)
VAR previous_year =
IF(r_month = "Jan", r_year - 1, r_year)
RETURN
CALCULATE(
COUNTROWS(BLADE_LOGIC_FIM),
FILTER(
BLADE_LOGIC_FIM,
BLADE_LOGIC_FIM[Reporting Year_fim] = previous_year &&
BLADE_LOGIC_FIM[Reporting Month_fim] = previous_month
)
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.