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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
jr3151006
Helper IV
Helper IV

Help with Measure (DAX) - filter to get the lastest 03 Quarter records

Hi,

 

we need count of records for a Table which belongs to last 03 quarters; I´m trying to using the code below but shows an error.

Can someone help me?

 

---

Measure01 = CALCULATE(
                                         COUNTROWS('Table1'),
                                         FILTER('Table1','Table1'[Data de propositura].[Quarter]=-3))

---

1 ACCEPTED SOLUTION
jr3151006
Helper IV
Helper IV

Since I was unable to find some help to it, I was able to get results using another way as described below:

 

1) Add 02 'Custom Collumn' to the table with help of 'PowerQuery M';

       A) First custom collumn was to find if the 'DateIsInCurrentQuarter' (=Date.IsInCurrentQuarter ([DateField]));

       B) Second custom collumn was  to find if the 'DateIsInPreviousQuarter' (=Date.IsInPreviousNQuarters(Date.AddQuarters([DateField], -1), 2));

2) Create a Measure with filters to ignore records in current quarter and also filter to obtain records where 'previous quarter' is true.

 

Take a look my DAX Measure:

######################

 

 

MeasurePreviousQuarter = CALCULATE(
                    COUNTROWS(Table1),
                    FILTER(
                        Table1,
                        Table1[DateIsInCurrentQuarter] = FALSE()
                        ),
                    FILTER(
                        Table1,
                        Table1[DateIsInPreviousQuarter] = TRUE()
                            )
                        )

 

View solution in original post

7 REPLIES 7
jr3151006
Helper IV
Helper IV

Since I was unable to find some help to it, I was able to get results using another way as described below:

 

1) Add 02 'Custom Collumn' to the table with help of 'PowerQuery M';

       A) First custom collumn was to find if the 'DateIsInCurrentQuarter' (=Date.IsInCurrentQuarter ([DateField]));

       B) Second custom collumn was  to find if the 'DateIsInPreviousQuarter' (=Date.IsInPreviousNQuarters(Date.AddQuarters([DateField], -1), 2));

2) Create a Measure with filters to ignore records in current quarter and also filter to obtain records where 'previous quarter' is true.

 

Take a look my DAX Measure:

######################

 

 

MeasurePreviousQuarter = CALCULATE(
                    COUNTROWS(Table1),
                    FILTER(
                        Table1,
                        Table1[DateIsInCurrentQuarter] = FALSE()
                        ),
                    FILTER(
                        Table1,
                        Table1[DateIsInPreviousQuarter] = TRUE()
                            )
                        )

 

Hi @jr3151006 

 

It seems you only have one table in the model. You can also try the following measure without adding new columns. It uses some Date functions. Date and time functions (DAX) - DAX | Microsoft Docs

MeasurePreviousQuarter =
VAR _currentQuarterStart =
    DATE ( YEAR ( TODAY () ), QUARTER ( TODAY () ) * 3 - 2, 1 )
VAR _previous3QuarterStart =
    EOMONTH ( _currentQuarterStart, -10 ) + 1
RETURN
    COUNTROWS (
        FILTER (
            'Table1',
            'Table1'[Date] >= _previous3QuarterStart
                && 'Table1'[Date] < _currentQuarterStart
        )
    )

 

Additionally, DATEADD is a Time Intelligence function. To use it, it is recommended to have a Date table in the model. Time intelligence functions (DAX) - DAX | Microsoft Docs

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Hi v-jingzhang,

I'll test this and ping back here in few hours. Tks for your help.

Hi @jr3151006 

 

Does my measure work? Is there any feedback?

 

Best Regards,
Jing

jr3151006
Helper IV
Helper IV

I did some change and now the error is:

 

"MdxScript(Model) (21, 51) Calculation error in measure 'Table1'[Measure067]: Cannot convert value 'Qtr 1' of type Text to type Number."

----


My Current DAX Measure

###################

Measure01 = CALCULATE(
                                         COUNTROWS('Table1'),
                                         FILTER('Table1','Table1'[DateField]='Table1'[DateField].[Quarter]-3))

Tahreem24
Super User
Super User

@jr3151006 Try the below DAX:

Measure = CALCULATE(COUNTROWS(Table1),DATEADD(Datecolumn,-3,QUARTER))

 

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

Hi Tahreem24,


tks for quick reply.
Almost there....

 

The result expected (for this test) was '0' since the dates on table are '08/07/2022', '09/07/2016' and '27/11/2020'. Pls, don't get me wrong but I realize that this code just ignores the current the current year.
Maybe we need to adjust the DAX to also consider the current year + quarter in the calc.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.