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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply

count of items basis 2 dates

I have 2 tables.....one is Calendar table and another is maindatabase.......in maindatabase table.....I have 3 columns.......

1. Items

2. Received date

3. Expiration date

 

Received date has primary relationship with Date column of calendar table.......Expiration date has secondary relationship with date column of Calendar table.......Now I want to count the distinct items.....basis the months and want to know they are expiring in which month.....so for an example......the woids that I received in the month of Jan are expiring in which months of year....similarly the woids that I received in the month of Feb are expiring in which month of year.....can anyone please help......

1 ACCEPTED SOLUTION

@sandeep_sharma There may be other methods, but I solved this using 2 calendar tables and the following measure. PBIX is attached below signature.

Measure = 
  VAR __Received = MAX('Calendar'[MonthSort])
  VAR __Expired = MAX('Expiration Calendar'[MonthSort])
  VAR __Table = FILTER(ALL('Table'), MONTH( 'Table'[Received date] ) = __Received && MONTH( 'Table'[Expiration Date] ) = __Expired )
  VAR __Products = DISTINCT( SELECTCOLUMNS( __Table, "__Item", [Item] ) )
  VAR __Result = COUNTROWS( __Products )
RETURN
  __Result

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

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

@sandeep_sharma You can use USERELATIONSHIP coupled with CALCULATE or CALCULATETABLE such as:

Measure =
  VAR __Table = CALCULATETABLE( 'maindatabase', USERELATIONSHIP( 'Calendar'[Date], 'maindatabase'[Expiration date] )
  VAR __Products = DISTINCT( SELECTCOLUMNS( __Table, "__ProductID", [ProductID] ) )
  VAR __Result = COUNTROWS( __Products )
RETURN
  __Result

Otherwise, please provide sample data and expected results.


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

Not sure if I can insert a file here....hence sharing the Snapshot of sample data......

sandeep_sharma_0-1716038480042.png

Also, below is what I need,

sandeep_sharma_1-1716038525636.png

Please be noted that I have a calendar table that has primary relationship with Received date column and secondary relationship with expiry date.......not sure what is the best way to show.....when I dont use calendar table and put received date in rows and expiry date in column....they dont show the hierarchy even when they have right data format.......

@sandeep_sharma Any chance you can post that sample data as text so I can copy and paste? I don't want to type all that.


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

 

ItemReceived dateExpiration Date
26366833/4/20243/4/2024
37921765/6/20246/5/2024
20542622/4/20243/5/2024
17612262/5/20243/6/2024
48843352/4/20244/4/2024
23187853/7/20244/6/2024
45488303/8/20244/7/2024
26075403/4/20244/3/2024
31005893/4/20244/3/2024
15709415/4/20245/4/2024
12285985/6/20246/5/2024
43568475/7/20246/6/2024
48556983/4/20244/3/2024
18950402/7/20243/8/2024
39049262/8/20242/8/2024
12529312/8/20242/8/2024
40073662/10/20243/31/2024
46100455/7/20246/6/2024
30209335/7/20246/6/2024
21501993/4/20244/3/2024

@sandeep_sharma There may be other methods, but I solved this using 2 calendar tables and the following measure. PBIX is attached below signature.

Measure = 
  VAR __Received = MAX('Calendar'[MonthSort])
  VAR __Expired = MAX('Expiration Calendar'[MonthSort])
  VAR __Table = FILTER(ALL('Table'), MONTH( 'Table'[Received date] ) = __Received && MONTH( 'Table'[Expiration Date] ) = __Expired )
  VAR __Products = DISTINCT( SELECTCOLUMNS( __Table, "__Item", [Item] ) )
  VAR __Result = COUNTROWS( __Products )
RETURN
  __Result

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

thanks @Greg_Deckler . This is very innovative solution. I saw this first time and got to learn  a new thing. 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Kudoed Authors