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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Mkrishna
Helper III
Helper III

DAX Not Working/ Date is not chaning

Hi All,
This is the data model

Mkrishna_1-1708148304197.png

 


The Product Table is connect to fact table by product key
Store list is connect to fact table by customer key

Date is connected to Fact table but not to store list


Fact table is a monthly data. It consist of sales rep, date, sales amount. I get the data on monthly basis and I append the data.
Store List consist of full list of store in the organization. This is also a monthly data. I append each month data.  The reason to append this data is I want to preserve the histrorical data.

 

My goal/aim

Total Customer : 

1. User select the date (say December), then it should filter Store List to Dec Data. 

2. Then Filter the Fact Table data from Oct 01 to Dec 31. 

3. Then find the common store between (1) and (2)

 

The code I used to find this is 

Customer Count =

VAR StartDate = STARTOFMONTH(DATEADD('Date'[Date],-2, MONTH))

VAR FinalDate = MAX('Date'[Date])

VAR Customers =
    CALCULATETABLE (
        VALUES ( 'Store List'[Store Number] ),
        TREATAS ( VALUES ( 'Date'[Date] ), 'Store List'[Date] ), 'Store List'[Date] =MIN('Date'[Date])
    )


VAR PriorCustomers = CALCULATETABLE(
    VALUES('Fact Table'[Store Number]),
    FILTER(
        ALL('Date'),
        'Date'[Date] >= StartDate &&
        'Date'[Date] <= FinalDate
    ),ALL('Product'), ALL('Store List'))

VAR CountCommonCustomers = COUNTROWS(INTERSECT(Customers, PriorCustomers))
RETURN
    CountCommonCustomers

  Note: This code works fine when the date is changed in date slicer.
 
 
Now I want to find if these store has Brand BB in the maximum date (Maximum date should be between Oct 01, and Dec 31). The maximum date is obtained from Fact Table. The code I used is as follows

Nos. of Store =

VAR _Reference_Date = MAX('Date'[Date])
VAR _Previous_Dates = DATESINPERIOD(
    'Previous Date'[Date],
    _Reference_Date,
    -3,
    MONTH
)

VAR MonthlyStore =
    CALCULATETABLE (
        VALUES ( 'Store List'[Store Number] ),
        TREATAS ( VALUES ( 'Date'[Date] ), 'Store List'[Date] ), 'Store List'[Date] =MIN('Date'[Date])
    )

VAR MaxDate =
    MAXX(
        FILTER(
            ALL('Fact Table'),
            'Fact Table'[Store Number] IN MonthlyStore &&
            'Fact Table'[Visit Date] IN _Previous_Dates
        ),
        'Fact Table'[Visit Date]
    )

VAR STORE_COUNT =
    CALCULATE(
        COUNTROWS(
            FILTER(
                'Fact Table',
                'Fact Table'[Store Number] IN MonthlyStore &&
                'Fact Table'[Visit Date] = MaxDate
            )
        ),
        REMOVEFILTERS('Date'),
        KEEPFILTERS(_Previous_Dates),
        USERELATIONSHIP('Date'[Date], 'Previous Date'[Date])
    )


RETURN IF(STORE_COUNT >0,1,0)
 
There is a problem in this code. It work fine for when I choose December. When I choose December, the Store list is also filtered to December month and Fact Table is filtered to Oct - Dec and gives me the correct number of store that has brand BB at their maxium date of visit. 

Problem:
When I select other date like October (other than December), the total customer count is correct but Nos. of Store does not give the correct value. I found that problem

1. When I select October, the Store List is filtered to October. I want Fact table to be filtered to Aug 01 - Oct 31 but this code only fitler Fact table to Oct only. So the Nos of Store is only given for Oct.

Request for help
1 ACCEPTED SOLUTION
v-binbinyu-msft
Community Support
Community Support

Hi @Mkrishna ,

It seems like you're facing a challenge with DAX date filtering when trying to calculate the number of stores with Brand BB for the maximum visit date within a specific period.

Let's address the problem step by step:

1. Filtering Store List to Selected Month: You've mentioned that when selecting a month other than December, the Store List correctly filters to that month. This is expected behavior as per your current DAX formula.

 

2. Filtering Fact Table to a 3-Month Period: The issue arises when you want the Fact Table to filter to a three-month period ending with the selected month. Your current code snippet seems to filter the Fact Table only for the selected month, not the entire period.

 

To resolve this, we need to adjust your DAX measure to ensure that it considers the entire three-month period. Here's a revised version of your 'Nos. of Store' measure:

Nos. of Store =
VAR SelectedEndDate = MAX('Date'[Date])
VAR StartDate = STARTOFMONTH(DATEADD(SelectedEndDate, -2, MONTH))
VAR EndDate = EOMONTH(SelectedEndDate, 0)
VAR MonthlyStore =
    CALCULATETABLE(
        VALUES('Store List'[Store Number]),
        TREATAS(VALUES('Date'[Date]), 'Store List'[Date]),
        'Store List'[Date] = MIN('Date'[Date])
    )
VAR MaxDate =
    CALCULATE(
        MAX('Fact Table'[Visit Date]),
        FILTER(
            ALL('Fact Table'),
            'Fact Table'[Store Number] IN MonthlyStore &&
            'Fact Table'[Visit Date] >= StartDate &&
            'Fact Table'[Visit Date] <= EndDate
        )
    )
VAR STORE_COUNT =
    CALCULATE(
        COUNTROWS(
            FILTER(
                'Fact Table',
                'Fact Table'[Store Number] IN MonthlyStore &&
                'Fact Table'[Visit Date] = MaxDate &&
                'Fact Table'[Brand] = "BB"
            )
        ),
        REMOVEFILTERS('Date'),
        KEEPFILTERS(VALUES('Date'[Date]))
    )
RETURN
    IF(STORE_COUNT > 0, 1, 0)

-Please note the following changes:

- Defined 'StartDate' and 'EndDate' to represent the start and end of the three-month period.
- Adjusted the 'FILTER' function within 'MaxDate' to consider the entire period between 'StartDate' and 'EndDate'.
- Added a condition to check for Brand BB in the 'STORE_COUNT' calculation.

Please try this revised measure and see if it produces the expected results.

 

Best regards,
Community Support Team_Binbin Yu
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-binbinyu-msft
Community Support
Community Support

Hi @Mkrishna ,

It seems like you're facing a challenge with DAX date filtering when trying to calculate the number of stores with Brand BB for the maximum visit date within a specific period.

Let's address the problem step by step:

1. Filtering Store List to Selected Month: You've mentioned that when selecting a month other than December, the Store List correctly filters to that month. This is expected behavior as per your current DAX formula.

 

2. Filtering Fact Table to a 3-Month Period: The issue arises when you want the Fact Table to filter to a three-month period ending with the selected month. Your current code snippet seems to filter the Fact Table only for the selected month, not the entire period.

 

To resolve this, we need to adjust your DAX measure to ensure that it considers the entire three-month period. Here's a revised version of your 'Nos. of Store' measure:

Nos. of Store =
VAR SelectedEndDate = MAX('Date'[Date])
VAR StartDate = STARTOFMONTH(DATEADD(SelectedEndDate, -2, MONTH))
VAR EndDate = EOMONTH(SelectedEndDate, 0)
VAR MonthlyStore =
    CALCULATETABLE(
        VALUES('Store List'[Store Number]),
        TREATAS(VALUES('Date'[Date]), 'Store List'[Date]),
        'Store List'[Date] = MIN('Date'[Date])
    )
VAR MaxDate =
    CALCULATE(
        MAX('Fact Table'[Visit Date]),
        FILTER(
            ALL('Fact Table'),
            'Fact Table'[Store Number] IN MonthlyStore &&
            'Fact Table'[Visit Date] >= StartDate &&
            'Fact Table'[Visit Date] <= EndDate
        )
    )
VAR STORE_COUNT =
    CALCULATE(
        COUNTROWS(
            FILTER(
                'Fact Table',
                'Fact Table'[Store Number] IN MonthlyStore &&
                'Fact Table'[Visit Date] = MaxDate &&
                'Fact Table'[Brand] = "BB"
            )
        ),
        REMOVEFILTERS('Date'),
        KEEPFILTERS(VALUES('Date'[Date]))
    )
RETURN
    IF(STORE_COUNT > 0, 1, 0)

-Please note the following changes:

- Defined 'StartDate' and 'EndDate' to represent the start and end of the three-month period.
- Adjusted the 'FILTER' function within 'MaxDate' to consider the entire period between 'StartDate' and 'EndDate'.
- Added a condition to check for Brand BB in the 'STORE_COUNT' calculation.

Please try this revised measure and see if it produces the expected results.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Greg_Deckler
Super User
Super User

@Mkrishna Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors