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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Govind98
Frequent Visitor

Get the Month with most no of complaints

Hi, 
I was working on a measure that will return the month with most complaints from the desired time range. I have created a  DateTable with columns Month, MonthName,MonthYear(eg: Jan 2019),Year,Quarter. In the dashboard I have provided the slicer for MonthYear as dropdown style which will be the anchor date. Then provided a parameter with numeric range for the user to enter the value for N. So The timeperiod should be from (Anchor date- N) to Anchor date. I want to create a card in order to display the month with most no of complaints along with year (eg: Jan 2024). For counting the complanit, you can use complaint_id column in the main table. 
DateTable:

Govind98_2-1731509240889.png

 


Expected Dashboard.

Govind98_1-1731509156338.png

I have searched online and tried chatGPT, but I cant get any answers

 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

Try

Month with Max Complaints =
VAR ReferenceDate =
    MAX ( 'Date'[Date] )
VAR ChosenMonths =
    SELECTEDVALUE ( 'Previous N Months'[Value] )
VAR StartDate =
    EOMONTH ( ReferenceDate, - ( ChosenMonths + 1 ) ) + 1
VAR MonthsWithValues =
    ADDCOLUMNS (
        CALCULATETABLE (
            VALUES ( 'Date'[Year month] ),
            DATESBETWEEN ( 'Date'[Date], StartDate, ReferenceDate )
        ),
        "@num complaints", CALCULATE ( DISTINCTCOUNT ( 'Table'[complaint_id] ) )
    )
VAR Result =
    CONCATENATEX (
        TOPN ( 1, MonthsWithValues, [@num complaints] ),
        'Date'[Year month],
        ", "
    )
RETURN
    Result

In the event of multiple months having the same highest number it will return them all in a comma separated list.

View solution in original post

3 REPLIES 3
johnt75
Super User
Super User

Try

Month with Max Complaints =
VAR ReferenceDate =
    MAX ( 'Date'[Date] )
VAR ChosenMonths =
    SELECTEDVALUE ( 'Previous N Months'[Value] )
VAR StartDate =
    EOMONTH ( ReferenceDate, - ( ChosenMonths + 1 ) ) + 1
VAR MonthsWithValues =
    ADDCOLUMNS (
        CALCULATETABLE (
            VALUES ( 'Date'[Year month] ),
            DATESBETWEEN ( 'Date'[Date], StartDate, ReferenceDate )
        ),
        "@num complaints", CALCULATE ( DISTINCTCOUNT ( 'Table'[complaint_id] ) )
    )
VAR Result =
    CONCATENATEX (
        TOPN ( 1, MonthsWithValues, [@num complaints] ),
        'Date'[Year month],
        ", "
    )
RETURN
    Result

In the event of multiple months having the same highest number it will return them all in a comma separated list.

I think it is returning the value as a table as in the image. I made some changes in the code. Name of the date table is DateTable and column name is MonthYear. Also the parameter name is N, so the value will be 

N[N Value].

Govind98_4-1731519385685.png

 

That looks like its working then.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors