The ultimate Microsoft Fabric, Power BI, Azure AI, and SQL learning event: Join us in Stockholm, September 24-27, 2024.
Save €200 with code MSCUST on top of early bird pricing!
Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
I have this table
I would like to add a column (desired result) that shows the country with the most users participating in that project for each project
As example, for project 1 I have 3 users in CA and 2 in US. "Desired Result" will then show CA for all users
UsersName | UsersCountry | Project | DesiredResult | |||
User1 | CA | Project1 | CA | |||
User2 | US | Project1 | CA | |||
User3 | CA | Project1 | CA | |||
User4 | CA | Project1 | CA | |||
User5 | CA | Project1 | CA | |||
User1 | CA | Project2 | US | |||
User2 | US | Project2 | US | |||
User3 | CA | Project2 | US | |||
User4 | US | Project2 | US | |||
User6 | US | Project2 | US | |||
User1 | CA | Project3 | AU | |||
User3 | CA | Project3 | AU | |||
User6 | US | Project3 | AU | |||
User7 | AU | Project3 | AU | |||
User8 | AU | Project3 | AU | |||
User9 | AU | Project3 | AU | |||
User10 | AU | Project3 | AU | |||
User11 | AU | Project3 | AU | |||
User12 | AU | Project3 | AU | |||
User13 | AU | Project3 | AU |
Solved! Go to Solution.
Desired Result =
VAR _current_project = Table[Project]
VAR _tbl_1 =
FILTER(
Table,
Table[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Table[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _desired_country =
CONCATENATEX(
FILTER(
_tbl_2,
[@Users] = _max_per_country
),
Table[UsersCountry], ", "
)
RETURN
_desired_country
In case it solved your question please mark this as a solution for community visibility. Appreciate Your Kudos 🙂
For example, let's say in case of a tie, you want to return the country by sorting of it's name.
So, is there will be a tie between CA and US, than the result will be CA:
Desired Result 2 =
VAR _current_project = Users[Project]
VAR _tbl_1 =
FILTER(
Users,
Users[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Users[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _top_countries =
FILTER(
_tbl_2,
[@Users] = _max_per_country
)
RETURN
MINX(_top_countries,Users[UsersCountry])
Ok, something "quick and dirty" 😄: (US is probably the latest alphabetical country in your data):
Desired Result 2 =
VAR _current_project = Users[Project]
VAR _tbl_1 =
FILTER(
Users,
Users[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Users[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _top_countries =
FILTER(
_tbl_2,
[@Users] = _max_per_country
)
RETURN
COALESCE(MAXX(_top_countries,Users[UsersCountry]), "US")
Hey, check out my showcase report:
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
If you like it, give it a thumbs up 🙂
Desired Result =
VAR _current_project = Table[Project]
VAR _tbl_1 =
FILTER(
Table,
Table[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Table[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _desired_country =
CONCATENATEX(
FILTER(
_tbl_2,
[@Users] = _max_per_country
),
Table[UsersCountry], ", "
)
RETURN
_desired_country
In case it solved your question please mark this as a solution for community visibility. Appreciate Your Kudos 🙂
Great! It does exactly want I wanted.
However, when there is a tie, it shows as example CA,US
How can I pick and show only the first one ?
@Sly I did that on porpuse :))
In case there is a tie, who is the first one? 😅
Give me the logic for that and I'll add a new measure for that but will also keep the previous one for community visabilty. Please don't forget to mark them as a solution
I want to use that for communications purpose and use the right language for an email.
As example, If I get US,SP I can either use english or Spanish.
But I would prefer to use English because it is a "universal language"
I know I can't handle all the exceptions, but I also know that a majority of people can communicate in English.
Also, I know that I have bunch of users without country set (BLANK). So if I end up with a majority displaying BLANK, I also want to use English by default for this case.
Ok, something "quick and dirty" 😄: (US is probably the latest alphabetical country in your data):
Desired Result 2 =
VAR _current_project = Users[Project]
VAR _tbl_1 =
FILTER(
Users,
Users[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Users[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _top_countries =
FILTER(
_tbl_2,
[@Users] = _max_per_country
)
RETURN
COALESCE(MAXX(_top_countries,Users[UsersCountry]), "US")
Hey, check out my showcase report:
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
If you like it, give it a thumbs up 🙂
I could use both of your solutions, but I think this one will do the trick !
Thank you very much, really appreciated !
For example, let's say in case of a tie, you want to return the country by sorting of it's name.
So, is there will be a tie between CA and US, than the result will be CA:
Desired Result 2 =
VAR _current_project = Users[Project]
VAR _tbl_1 =
FILTER(
Users,
Users[Project] = _current_project
)
VAR _tbl_2 =
GROUPBY( _tbl_1,Users[UsersCountry],"@Users", SUMX(CURRENTGROUP(), 1))
VAR _max_per_country = MAXX(_tbl_2, [@Users])
VAR _top_countries =
FILTER(
_tbl_2,
[@Users] = _max_per_country
)
RETURN
MINX(_top_countries,Users[UsersCountry])
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
Check out the August 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
115 | |
79 | |
78 | |
44 | |
38 |
User | Count |
---|---|
150 | |
116 | |
68 | |
64 | |
58 |