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
SamOvermars
Helper I
Helper I

Dax measure to calculate current year if no filter is selected

Hello, 

I have a slicer that has the following column 

MY = FORMAT(DimDate[FullDate], "mmm-yy")
It will filter the dashboard based on the month and year.
I need to create a card that will show the Current month to budget and the month will be changed based on the above slicer.
For example if I pick July-21 it will compare the july21 data to Julys budget.
so far I created this measure for the card

Var CurrentMonth = CALCULATE(SUM(Table[Price]), FILTER(DimDate, MONTH(DimDate[FullDate]) = MONTH(TODAY()) && YEAR(DimDate[FullDate] = YEAR(TODAY() ))))
Var Budget = calculate(SUM(Budget[MonthlyBudget]))

Return CurrentMonth/Budget

 

but for somereason then my slicer will work only if something is selected. I want the card to show current year and month data if nothing is selected in the slicer and will dynamically change if something is selected in the slicer. How can I do that ?

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @SamOvermars ,

According to your description, in your formula, the below code only related to the current year and month but not the value selected in the slicer. Also, the Budget table will also filtered by the slicer, if you want to show current year and month data if nothing is selected in the slicer, you should also add filter in the variable Budget.

FILTER(DimDate, MONTH(DimDate[FullDate]) = MONTH(TODAY()) && YEAR(DimDate[FullDate] = YEAR(TODAY() ))

I create a sample.

Table:

vkalyjmsft_0-1658828207913.png

DimDate table:

vkalyjmsft_1-1658828242007.png

Budget table:

vkalyjmsft_2-1658828357085.png

Here's my solution, create a measure.

Measure =
VAR _T1 =
    FILTER (
        DimDate,
        MONTH ( DimDate[FullDate] ) = MONTH ( MAX ( 'DimDate'[FullDate] ) )
            && YEAR ( DimDate[FullDate] = YEAR ( MAX ( 'DimDate'[FullDate] ) ) )
    )
VAR _T2 =
    FILTER (
        DimDate,
        MONTH ( DimDate[FullDate] ) = MONTH ( TODAY () )
            && YEAR ( DimDate[FullDate] = YEAR ( TODAY () ) )
    )
VAR _CurrentMonth =
    IF (
        ISFILTERED ( DimDate[MY] ),
        CALCULATE ( SUM ( 'Table'[Price] ), _T1 ),
        CALCULATE ( SUM ( 'Table'[Price] ), _T2 )
    )
VAR _Budget =
    IF (
        ISFILTERED ( DimDate[MY] ),
        CALCULATE ( SUM ( 'Budget'[MonthlyBudget] ), _T1 ),
        CALCULATE ( SUM ( 'Budget'[MonthlyBudget] ), _T2 )
    )
RETURN
    _CurrentMonth / _Budget

Get the correct result.

vkalyjmsft_3-1658828439454.png

vkalyjmsft_4-1658828469365.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @SamOvermars ,

According to your description, in your formula, the below code only related to the current year and month but not the value selected in the slicer. Also, the Budget table will also filtered by the slicer, if you want to show current year and month data if nothing is selected in the slicer, you should also add filter in the variable Budget.

FILTER(DimDate, MONTH(DimDate[FullDate]) = MONTH(TODAY()) && YEAR(DimDate[FullDate] = YEAR(TODAY() ))

I create a sample.

Table:

vkalyjmsft_0-1658828207913.png

DimDate table:

vkalyjmsft_1-1658828242007.png

Budget table:

vkalyjmsft_2-1658828357085.png

Here's my solution, create a measure.

Measure =
VAR _T1 =
    FILTER (
        DimDate,
        MONTH ( DimDate[FullDate] ) = MONTH ( MAX ( 'DimDate'[FullDate] ) )
            && YEAR ( DimDate[FullDate] = YEAR ( MAX ( 'DimDate'[FullDate] ) ) )
    )
VAR _T2 =
    FILTER (
        DimDate,
        MONTH ( DimDate[FullDate] ) = MONTH ( TODAY () )
            && YEAR ( DimDate[FullDate] = YEAR ( TODAY () ) )
    )
VAR _CurrentMonth =
    IF (
        ISFILTERED ( DimDate[MY] ),
        CALCULATE ( SUM ( 'Table'[Price] ), _T1 ),
        CALCULATE ( SUM ( 'Table'[Price] ), _T2 )
    )
VAR _Budget =
    IF (
        ISFILTERED ( DimDate[MY] ),
        CALCULATE ( SUM ( 'Budget'[MonthlyBudget] ), _T1 ),
        CALCULATE ( SUM ( 'Budget'[MonthlyBudget] ), _T2 )
    )
RETURN
    _CurrentMonth / _Budget

Get the correct result.

vkalyjmsft_3-1658828439454.png

vkalyjmsft_4-1658828469365.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

Migasuke
Super User
Super User

Hi @SamOvermars ,

To apply the logic when something is filtered is good to  use ISFILTERED and IF. So basically you should have your measure for latest period and for selection. Then you only need to use those two measures and combine it with functions above.

Some sample measure looks like this:

MyMeasure =
var _SelectedRevenue = SUM('Table'[REVENUE])
var _LatestMonth = CALCULATE(_SelectedRevenue,LASTDATE('Table'[DateColum]))

Return

IF(
ISFILTERED('DimColumn'[MMM-YYYY]),
                 _SelectedRevenue,
                _LatestMonth
)



If my answer was helpful please give me a Kudos or even accept as a Solution.

Let's connect on LinkedIn!

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