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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
AnthonyJoseph
Resolver III
Resolver III

Calculate only if value is there for all selected years

Hello Community,

 

I have a multi-year fees data set for different countries. Year field is used in the slicer, so when the user selects multiple years then the calculation should display only the sum of fess that are for the countries that have all the selected years.

 

For example: In the below dataset, when the years "2020 and 2021" are selected, then sum of fees should be calculated only for the countries that have "Fees" for the years "2020 and 2021".

i.e. Countries that have fees for both 2020 and 2021 are Argentina, India, and China, so sum of fees = 349620

 

IDCountryEntityYearFees
1IndiaA202021883
2ArgentinaB202087936
3ChinaC202067194
4IndiaA202130482
5BrazilD20214435
6ArgentinaB202172670
7ChinaC202169455
8IndiaA20226470
9BrazilD202248177
10ChinaC20225338

 

Please can someone help me how to achieve this in power bi?

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1659173157625.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

4 REPLIES 4
v-yangliu-msft
Community Support
Community Support

Hi  @AnthonyJoseph ,

Here are the steps you can follow:

1. Create measure.

Measure =
var _select1=MINX(ALLSELECTED('Table'),[Year])
var _select2=MaxX(ALLSELECTED('Table'),[Year])
var _table1=
FILTER(ALL('Table'),'Table'[Year]in {_select1,_select2})
var _table2=
ADDCOLUMNS(_table1,"count",COUNTX(FILTER(_table1,[Country]=EARLIER('Table'[Country])),[Country]))
return
SUMX(FILTER( _table2,[count]>=2),[Fees])

2. Result:

vyangliumsft_0-1659405310523.png

If you need pbix, please click here.

 

Best Regards,

Liu Yang

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

CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1659173157625.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

daXtreme
Solution Sage
Solution Sage

 

 

Total Fees = 
var SelectedYears = ALLSELECTED( T[Year] )
var CountriesWithFeesInAllSelectedYears =
    filter(
        distinct( T[Country] ),
        calculate(
            isempty(
                except(
                    SelectedYears,
                    distinct( T[Year] )
                )
            )
        )
    )
var Result =
    calculate(
        sum( T[Fees] ),
        CountriesWithFeesInAllSelectedYears
    )
return
    Result

 

 

tamerj1
Super User
Super User

Hi @AnthonyJoseph 
Here is a sample file with the solution https://www.dropbox.com/t/B7QvosI10ak7l6hg

Create a filter table as follows

1.png

The create a filter measure, place in the filter pane of the visual, select "Is not blank" and apply the filter

2.png3.png4.png

 

Filter Measure = 
VAR SelectedYears = VALUES ( 'Year'[Year] )
VAR SelectedCountries = 
    CALCULATETABLE ( 
        VALUES ( Data[Year] ),
        TREATAS ( SelectedYears, Data[Year] ),
        ALLEXCEPT ( Data, Data[Country] )
    )
RETURN
    IF (
        COUNTROWS ( SelectedCountries ) 
            = COUNTROWS ( SelectedYears ),
        1
    )

Other method using EXCEPT function as follows

1.png

Filter Measure 2 = 
VAR SelectedYears = VALUES ( 'Year'[Year] )
VAR SelectedCountries = 
    CALCULATETABLE ( 
        VALUES ( Data[Year] ),
        TREATAS ( SelectedYears, Data[Year] ),
        ALLEXCEPT ( Data, Data[Country] )
    )
RETURN
    COUNTROWS ( 
        EXCEPT ( SelectedYears, SelectedCountries )
    )

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors