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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
RichOB
Helper IV
Helper IV

Measure for date range and TopN

Hi, using the table below, I need a measure showing me the Top1 Location with the most "Medical Concerns" between 02/08/2024 = 01/11/2024 and the count, please = Edinburgh, 4.

 

Again I need this as a measure not just the visual filters, please.

 

Incident TypeLocationDateFinancial Quarter
Missing personGlasgow02/08/2024Q2
Missing personGlasgow02/08/2024Q2
Medical concernGlasgow01/11/2024Q3
Missing personGlasgow01/11/2024Q3
Missing personGlasgow01/11/2024Q3
Medical concernEdinburgh02/08/2024Q2
Medical concernEdinburgh02/08/2024Q2
Missing PersonEdinburgh02/08/2024Q2
Medical concernEdinburgh01/11/2024Q3
Medical concernEdinburgh01/11/2024Q3
AggressionManchester02/08/2024Q2
AggressionManchester02/08/2024Q2
AggressionManchester01/11/2024Q3
AggressionManchester01/11/2024Q3
Medical concernManchester01/11/2024Q3

 

Thanks for your help.

1 ACCEPTED SOLUTION

Hi @RichOB ,

bhanu_gautam  and Kedar have provided effective methods to obtain both the location and the top 1 count. You can make a slight adjustment to the measure below to include only the location:

Location with Top 1 Count = 
VAR _tab =
    SUMMARIZE (
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Incident Type] = "Medical concern" ),
        'Table'[Location],
        "@Count", COUNT ( 'Table'[Incident Type] )
    )
VAR _tab2 =
    TOPN ( 1, _tab, [@Count], DESC )
RETURN
    CONCATENATEX( _tab2, [Location],", " ,[Location],ASC)

vyiruanmsft_0-1733290113320.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
SacheeTh
Advocate III
Advocate III

You can create a DAX measure to find the top location with the most "Medical Concerns" in the given date range by combining several functions, such as CALCULATE, FILTER, and TOPN.

Here's a DAX measure that should help you achieve your goal:

Top1 Location with Medical Concerns =
VAR StartDate = DATE(2024, 8, 2)
VAR EndDate = DATE(2024, 11, 1)

VAR FilteredTable =
    FILTER (
        YourTableName,
        [Incident Type] = "Medical concern" &&
        [Date] >= StartDate &&
        [Date] <= EndDate
    )
VAR GroupedCount =
    SUMMARIZE (
        FilteredTable,
        YourTableName[Location],
        "MedicalConcernsCount", COUNTROWS ( FilteredTable )
    )
VAR TopLocation =
    TOPN ( 1, GroupedCount, [MedicalConcernsCount], DESC )

RETURN
IF (
    COUNTROWS ( TopLocation ) > 0,
    CONCATENATEX (
        TopLocation,
        YourTableName[Location] & ", " & [MedicalConcernsCount],
        ", "
    ),
    "No data"
)

Explanation:

  1. Define Start and End Dates:

    • StartDate and EndDate are set to 02/08/2024 and 01/11/2024, respectively.
  2. FilteredTable:

    • This variable filters the data to include only rows where the Incident Type is "Medical concern" and where the Date falls between the specified start and end dates.
  3. GroupedCount:

    • SUMMARIZE is used to group the filtered table by Location and count the number of rows (COUNTROWS) for each location.
  4. TopLocation:

    • TOPN is used to get the location with the highest count of "Medical concern" incidents.
  5. RETURN:

    • If there is a top location, CONCATENATEX returns the location and count as a string.
    • If there's no data in the given date range, it returns "No data".

This measure should give you the location with the most medical concerns within the given time frame, along with the count.

Kedar_Pande
Community Champion
Community Champion

@RichOB 

Top1LocationMedicalConcerns = 
VAR FilteredData =
FILTER(
'Table',
'Table'[Incident Type] = "Medical concern" &&
'Table'[Date] >= DATE(2024, 8, 2) &&
'Table'[Date] <= DATE(2024, 11, 1)
)
VAR Summary =
SUMMARIZE(
FilteredData,
'Table'[Location],
"MedicalConcernsCount", COUNTROWS(FilteredData)
)
VAR TopLocation =
TOPN(1, Summary, [MedicalConcernsCount], DESC)
RETURN
CONCATENATEX(TopLocation, 'Table'[Location], ", ") & ", " &
MAXX(TopLocation, [MedicalConcernsCount])

In your case, it will return Edinburgh, 4.

 

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

bhanu_gautam
Super User
Super User

@RichOB ,To create a measure in Power BI that shows the top location with the most "Medical Concerns" between 02/08/2024 and 01/11/2024, 

 

Top1LocationMedicalConcerns =
VAR FilteredTable =
FILTER(
'Table',
'Table'[Incident Type] = "Medical concern" &&
'Table'[Date] >= DATE(2024, 8, 2) &&
'Table'[Date] <= DATE(2024, 11, 1)
)
VAR GroupedTable =
SUMMARIZE(
FilteredTable,
'Table'[Location],
"MedicalConcernsCount", COUNT('Table'[Incident Type])
)
VAR TopLocation =
TOPN(
1,
GroupedTable,
[MedicalConcernsCount],
DESC
)
RETURN
CONCATENATEX(
TopLocation,
'Table'[Location] & ", " & [MedicalConcernsCount],
", "
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi @bhanu_gautam this is great thank you!

Sorry to be a pain but what would the measure look like if I didn't want the count number attached, Just the name of the top incident type, please?

Hi @RichOB ,

bhanu_gautam  and Kedar have provided effective methods to obtain both the location and the top 1 count. You can make a slight adjustment to the measure below to include only the location:

Location with Top 1 Count = 
VAR _tab =
    SUMMARIZE (
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Incident Type] = "Medical concern" ),
        'Table'[Location],
        "@Count", COUNT ( 'Table'[Incident Type] )
    )
VAR _tab2 =
    TOPN ( 1, _tab, [@Count], DESC )
RETURN
    CONCATENATEX( _tab2, [Location],", " ,[Location],ASC)

vyiruanmsft_0-1733290113320.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.