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
MichaelH78
Frequent Visitor

Measure that counts in second column

Hi, I have a table with three columns:
- Person number
- Primary residence (city)
- Secondary residence (city)

Person numberPrimary residence (city)Secondary residence (city)
1City 1City 2
2City 2City 1
3City 1City 3
4City 1 
5City 3City 1
6City 2City 3

 

Now I am looking for a measure that determines the number of data records that have the city in their primary or secondary residence. The first column in the matrix visual (or in the Excel pivot table) contains a list of primary residence locations.

CityCountExplanation
City 153 times primary, 2 times secondary
City 232 times primary, 1 time secondary
City 331 time primary, 2 times secondary

 

Thank you
Michael

1 ACCEPTED SOLUTION

I have solved it simply: With an additional column in data, which calculates the number per primary residence location, but in primary and secondary residence. This means that all rows with the same location always contain the same value.
The measure then only needs to add a MAX (or MIN or AVG) to the new column 😉

View solution in original post

5 REPLIES 5
v-jiewu-msft
Community Support
Community Support

Hi @MichaelH78 ,

First of all, many thanks to  for your very quick and effective replies.

Based on my testing, please try the following methods:

1.Create the simple table.

vjiewumsft_0-1717483900614.png

vjiewumsft_1-1717483917301.png

 

2.Create the new measure to calculate the number of city primary residence.

Count primary city = 
var cit_ = SELECTEDVALUE('Table city'[City])
RETURN
COUNTROWS(FILTER('Table', 'Table'[Primary residence (city)] = cit_))
Count secondary city = 
var cit_ = SELECTEDVALUE('Table city'[City])
RETURN
COUNTROWS(FILTER('Table', 'Table'[Secondary residence (city)] = cit_))

3.Create the new measure to calculate the count.

Count = [Count primary city] + [Count secondary city]

4.Drag the category field into the slicer.

vjiewumsft_2-1717483923374.png

5.The result is shown below.

vjiewumsft_3-1717483932188.png

 

Best Regards,

Wisdom Wu

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

 

Hi Wisdom Wu,

hi, thanks for your help. This looks very good and also works in Power BI.
Before I transfer everything to Power BI: Does this also work in Excel? Power Pivot in Excel does not recognise SELECTEDVALUE. You can probably do something with HASONEVALUE, but I can't manage that 😉

Best regards

Michael

I have solved it simply: With an additional column in data, which calculates the number per primary residence location, but in primary and secondary residence. This means that all rows with the same location always contain the same value.
The measure then only needs to add a MAX (or MIN or AVG) to the new column 😉

lbendlin
Super User
Super User

Why should that be a measure?  Sounds like this data is immutable. So it can be done in Power Query.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtKs7PU8grzU1KLVLSUQooysxNLKpUKEotzkxJzUtOVdBIziyp1ARKBacm5+elYJWM1YlWMgQqcQbyFOAMI7C4EYILVwASN8ZQbwwWN0EWVwALmSJUoBhhhmE00IhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Person number"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Value"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

 

 

Hello lbendlin,
Thank you for your reply. I have omitted some (many) columns in the sample data. So the data is mutable. It could be that the persons are filtered e.g. by year of birth or by gender etc., in which case the number should still be calculated correctly. As I understand it, I can only do this with a measure, right?

Best regards

Michael

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

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.