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
ryan_mayu
Super User
Super User

How to use filter in two related tables?

Hi , i have two tables. One is dimension date. The other one is fact table. Two tables have already created one to many relationship.

I want to get the Inventory amount of the last month. Can anyone help me fix the DAX below? Then if I choose Jan,Feb and Mar, the inventory will be 300.

Inventory = calculate(sum('fact'[Amount]),'fact'[TYPE]="Inventory"&&LASTDATE(RELATED('Dim_date'[date]))

1.JPG2.JPG

 





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

Proud to be a Super User!




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

Hi @ryan_mayu,

 

Based on my test, I made one sample for your reference here.

 

1. Enter the data as you shared and create relationship between tables as below.

Capture.PNG

 

2. Create a calculated column in Dim_date table.

Month no = MONTH('Dim_date'[Date])

3.Create a calculated column in the fact table.

 

monno = RELATED(Dim_date[Month no])

4. Create a measure to achieve your goal.

 

Measure = calculate(sum('FACT'[Amount]),FILTER(ALLSELECTED('FACT'),'fact'[TYPE]="Inventory"&&  'FACT'[monno] = CALCULATE(MAX('FACT'[monno]),ALLSELECTED('FACT'))))

2.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

6 REPLIES 6
v-frfei-msft
Community Support
Community Support

Hi @ryan_mayu,

 

Based on my test, I made one sample for your reference here.

 

1. Enter the data as you shared and create relationship between tables as below.

Capture.PNG

 

2. Create a calculated column in Dim_date table.

Month no = MONTH('Dim_date'[Date])

3.Create a calculated column in the fact table.

 

monno = RELATED(Dim_date[Month no])

4. Create a measure to achieve your goal.

 

Measure = calculate(sum('FACT'[Amount]),FILTER(ALLSELECTED('FACT'),'fact'[TYPE]="Inventory"&&  'FACT'[monno] = CALCULATE(MAX('FACT'[monno]),ALLSELECTED('FACT'))))

2.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

@v-frfei-msft

 

Hi , one more futher request for this.

 

If the data is

 

customer    Type          Month    Amount  

AAA           Inventory     Jan         100

AAA           Inventory     Feb        150

AAA           Inventory     Mar         120

BBB           Inventory      Jan         110

BBB           Inventory     Feb        100

BBB          Inventory       Mar         100

CCC          Inventory       Jan         200

CCC           Inventory     Feb         160

CCC           Inventory     Mar        100

 

 

If I used the DAX formula you provided, In the report view, the result is all the customer's inventory amount is the same, like below. Each customer will show the summary amount of last month.

 

Customer  Amount

AAA        320

BBB         320

CCC        320

 

How to modify the DAX formula to make sure we assign the correct amount to each customer?

 

Thanks in advanced.

 





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

Proud to be a Super User!




Hi @ryan_mayu,

 

Please update the formula as below.

 

Measure =
CALCULATE (
    SUM ( 'FACT'[Amount] ),
    FILTER (
        'FACT',
        'FACT'[Type] = "Inventory"
            && 'FACT'[MOn]
                = CALCULATE ( MAX ( 'FACT'[MOn] ), ALLEXCEPT ( 'FACT', 'FACT'[customer] ) )
    )
)

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

@v-frfei-msft

It works. Thanks a lot.





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

Proud to be a Super User!




Greg_Deckler
Super User
Super User

Your formulas aren't jiving with the images you show. 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

 

That being said, See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 

I'm still not sure what you are going for, you want Jan, Feb and March to all show 300?


@ 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...

Hi Greg, Thank for your reply. For inventory data, what we want to focus is the latest data. Inventory data can't be accumulated. That's why when we choose Jan, Feb, March, the inventory data will only show the data for Mar. The same logic to whole data. If I choose twelve month, I want the inventory measure show the Dec's inventory data. That's why I use lastdate formula.

 

Thanks a lot





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

Proud to be a Super User!




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.