cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
annguyenjoh
Frequent Visitor

SUM different columns based on selected value(s)

Hi,

I have a Date table like this

DateYear-MonthSwedenDenmarkNorway
2020-01-012020-01010
2020-01-022020-01010
2020-01-032020-01110
2020-01-042020-01111
2020-01-052020-01001
2020-01-062020-01001
2020-01-072020-01111
2020-01-082020-01110
2020-01-092020-01111
2020-01-102020-01110

 

Then I have the Company table like this

 

CompanyCountry
Swedish CompanySweden
Danish CompanyDenmark
Norwegian CompanyNorway

 

Would like to calculate Sales per working day for each company based on the total working day from the calendar. So I did the following measures:

 

Working day = SWITCH(TRUE(),
    SELECTEDVALUE(dimCompany[Country])="Sweden",SUM(dimDate[Sweden]),
    SELECTEDVALUE(dimCompany[Country])="Norway",SUM(dimDate[Norway]),
    SELECTEDVALUE(dimCompany[Country])="Denmark",SUM(dimDate[Denmark]),
    0)

Sales per working day = DIVIDE([Sales],[Working day],0)

 

 

It works well if I just choose one country... of course. So if I would like to choose more than one country, how should I write my DAX?

 

Example: If I pick Swedish and Danish Company, I want to see the result of: (SUM(dimDate[Sweden])+SUM(dimDate[Denmark]))/2

If I pick 3 companies, I want to see the result of (SUM(dimDate[Sweden])+SUM(dimDate[Denmark])+SUM(dimDate[Norway]))/3

 

Thank you very much.

1 REPLY 1
Greg_Deckler
Super User IV
Super User IV

@annguyenjoh Well, you could use the IN operator like

 

"Sweden" IN DISTINCT('Company'[Country]) && "Norway" IN DISTINCT('Company'[Country])

 

That said, it would be far better to unpivot the country columns in your first table and create a relationship between your two tables and then there is zero need for fancy DAX.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Get Ready for Power BI Dev Camp

Microsoft named a Leader in The Forrester Wave

Microsoft received the highest score of any vendor in both the strategy and current offering categories.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.