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
HCK
Frequent Visitor

DAX - how to filterout rows based on two (or more) identical columns

Hello,

I am relativelly new to DAX and I'm trying to figure out filtering of duplicate values. I have data (Qty) for more materials (PN=Part numbers) for different dates. there can be more items for one PN and one date.  I need to calculate day by day how many items per PN are available - running total. 
On top of that I need to recalculate that to a different unit imagine a box. Each PN can fit different qty to a box.
Lastly every PN belongs to a family and the aim is to know how many boxes will be occupied per family each day.

Movements:

Planned_dateEnd datePNIndexData1Datax QtyM1M2=M1/UM
28.09.202203.10.2022A1  10102
28.09.202229.09.2022B7  10010050
30.09.202204.10.2022B8  610653
04.10.202205.10.2022A2  20306
05.10.202205.10.2022A3  -5255
05.10.202205.10.2022A4  -3224,4
05.10.202205.10.2022A5  7153
05.10.202209.10.2022B9  -505628
06.10.202231.12.2024A6  10255
10.10.202231.12.2024B10  167236

 

UnitsUM
A5
B2

 

Family 
AFamily 1
BFamily 2
CFamily 1
Family 3

 

Target:

M328.09.202229.09.202230.09.202201.10.202202.10.202203.10.202204.10.202205.10.202206.10.202207.10.202208.10.202209.10.202210.10.202211.10.2022
A22222263555555
B5050535353535328282828283636
Total5252555555555931333333334141

 

I succesfully created a calculated column M1:

M1 =
  var CurrentDate = Movements[Planned_Date]
  var CurrentPN= Movements[PN]
  var CurrentIndex = Movements[Index]
  var Filteredtable = FILTER(Movements, Movements[Planned_Date]<=CurrentDate && Movements[PN]=CurrentPN   && Movements[Index]<=CurrentIndex)
Return
calculate(SUM(Movements[Qty]), Filteredtable)

as well as measure M2

M2 = Calculate(SUMX(Movements,Movements[M1]/Related(Units[UM])))


but I am struggling with M3 with the duplicate lines (Index 3 & 4)

M3 =
var Valbydate =CALCULATE (
               MAXX(
                    Filter(
                                    CROSSJOIN(Movements,'Date'),
                                    'Date'[Date] >= 'Movements'[Planned_Date] && 'Date'[Date]<= 'Movements'[EndDate]
                                    ),
                           Movements[Running total]
                    )
)
Return
    if(isblank(Valbydate),0,Valbydate)
If I use Maxx it works oK on PN level, but of course wrong on Family level.
I guess to use SUMX but before that I would need to somehow filter out "duplicate" lines with index 3 & 4. I suppose somehow with DISTINCT or SUMMARIZE but can't guess how.

As my running total M1 is based on DAX, I am looking for a DAX sollution.

I would appreciate any advice a lot.
Thank you in advance.

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

Hi @HCK ,

I created a sample pbix file(see attachment), please check if that is what you want.

yingyinr_0-1664775312593.png

If the above one can't help you get the desired result, please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @HCK ,

I created a sample pbix file(see attachment), please check if that is what you want.

yingyinr_0-1664775312593.png

If the above one can't help you get the desired result, please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

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

Thank you very much for your advice! You should me the logic to solve my issue. It seems that with a little adjustments of the columns and the measure I get to the required results! I want to verify on more data tomorrow and Accept your advice as a solution.

Thank you a lot!

HCK
Frequent Visitor

So Finally I get to my sollution. The major breakthrough was your setup of the New Index column.

That was basically my initial wish:

"how to filterout rows based on two (or more) identical columns"

Answer: using your New index and then fitering the table by: NOT isblank

 

Details: I used the New Index to filterout my table using:

Reduced = Calculatetable(
                            Movements, Filter(Movements, NOT ISBLANK(Movements[New Index])
                            ))
And on that table I performed the Measure:
M4 =
VAR _seldate =
    SELECTEDVALUE ( 'Date'[Date] )
VAR _selunits =
    SELECTEDVALUE ( 'Units'[Units] )
RETURN
    CALCULATE (
        SUM ( Reduced[M2]),
        FILTER (
            Reduced,
            Reduced[Planned_Date] <= _seldate
                && Reduced[EndDate] >= _seldate
                && Reduced[Index]=Reduced[New Index]))
And by that I got my wanted results.
Thank you very much!

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