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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Get the largest value of the selected filter values

I have a Quarter filter with values YearQ# (see table below). If I select 4 values, (e.g. 2022Q2-2023Q1), how do I get the largest SELECTEDVALUE (2023Q1) and the second largest SELECTEDVALUE (2022Q4) in DAX?

 

Quarter
2022Q1
2022Q2
2022Q3
2022Q4
2023Q1
2023Q2

 

1 ACCEPTED SOLUTION
Ahmedx
Super User
Super User

You need to create your own calendar

Calendar = 
VAR BaseCalendar =
    CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) )
RETURN
    GENERATE (
        BaseCalendar,
        VAR BaseDate = [Date]
        VAR YearDate = YEAR ( BaseDate )
        VAR MonthNumber = MONTH ( BaseDate )
        VAR YearMonthNumber = YearDate * 12 + MonthNumber - 1
        RETURN ROW (
            "Year", YearDate,
            "Quarter_Year",YearDate & "Q"&QUARTER([Date]),
            "Sort",YearDate*100+QUARTER([Date])
    )
)

and then determine the maximum minimum

MinQuarter = VAR _MinSelect = MIN('Calendar'[Sort])
RETURN
 MINX(FILTER(ALL('Calendar'),'Calendar'[Sort]=_MinSelect),[Quarter_Year])

MaxQuarter = VAR _MinSelect = MAX('Calendar'[Sort])
RETURN
 MINX(FILTER(ALL('Calendar'),'Calendar'[Sort]=_MinSelect),[Quarter_Year])

Sample PBIX file attached
https://1drv.ms/u/s!AiUZ0Ws7G26RhgrLydtoJOrm3tgv?e=KHDnMg
2023-03-01_11-44-47.png

View solution in original post

3 REPLIES 3
Ahmedx
Super User
Super User

why don't you want to create a separate calendar
Share some data to work with

Ahmedx
Super User
Super User

You need to create your own calendar

Calendar = 
VAR BaseCalendar =
    CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) )
RETURN
    GENERATE (
        BaseCalendar,
        VAR BaseDate = [Date]
        VAR YearDate = YEAR ( BaseDate )
        VAR MonthNumber = MONTH ( BaseDate )
        VAR YearMonthNumber = YearDate * 12 + MonthNumber - 1
        RETURN ROW (
            "Year", YearDate,
            "Quarter_Year",YearDate & "Q"&QUARTER([Date]),
            "Sort",YearDate*100+QUARTER([Date])
    )
)

and then determine the maximum minimum

MinQuarter = VAR _MinSelect = MIN('Calendar'[Sort])
RETURN
 MINX(FILTER(ALL('Calendar'),'Calendar'[Sort]=_MinSelect),[Quarter_Year])

MaxQuarter = VAR _MinSelect = MAX('Calendar'[Sort])
RETURN
 MINX(FILTER(ALL('Calendar'),'Calendar'[Sort]=_MinSelect),[Quarter_Year])

Sample PBIX file attached
https://1drv.ms/u/s!AiUZ0Ws7G26RhgrLydtoJOrm3tgv?e=KHDnMg
2023-03-01_11-44-47.png

Anonymous
Not applicable

This works! However, the Quarter column I provided is part of a larger table with more columns and corresponding values that I will be needing for creating more DAXs. Is there a way where I turn my Quarter column into a Calendar data (so that the values from other columns of specific Quarter values from my original table remain)?

 

To visualize:

Table:

QuarterPhonePhoneSold
22Q4A1
22Q4A3
22Q4B2
22Q4B2
23Q1A5
23Q1B1
23Q1A3
23Q1B3

Resulting Table:

Filtered on this visual: 22Q2, 22Q3, 22Q4, 23Q1 (or some other 4 quarters)

Phone Model

Sum of PhonesSold

(for the largest/recent quarter - 23Q1)

Sum of PhonesSold

(for the second largest/recent quarter - 22Q4)

DifferenceSparkline (hence the filter)
A835(sparkline in 4 quarters)
B440(sparkline in 4 quarters)

 

I'm planning to update the resulting table by only changing the Quarter filters and not creating more DAX.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors