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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
chuongnq
Regular Visitor

Power BI: Calculating Product_Code and Days Matching Max Date by Type and Group

I have a dataset A with the following columns:

  • Date: Date of the record.
  • Product_Code: Product code.
  • Type: Product type (e.g., X, Y).
  • Group: Product group (e.g., M, N).

Objective:

  1. Users will use a slicer to filter the dataset based on the Date range.
  2. After the user applies the slicer, the following calculations need to be performed:
    • Identify the Max(Date) for each combination of (Type, Group) within the selected date range.
    • Return the Product_Code corresponding to the Max(Date) for each (Type, Group).

Calculate the number of days matching the Max(Date) for each Product_Code.

Example:

Input (Dataset A - Original Data):


Date Product_Code Type Group

01/01/2024AYM
02/01/2024BYM
03/01/2024CXM
04/01/2024AXM
05/01/2024BXN
06/01/2024CXN
07/01/2024AYN
08/01/2024BYN

Case 1: User selects Date range (01/01/2024 - 08/01/2024):

Intermediate Table (Max(Date) by Type and Group):

Type Group Max_Date_By_Type_Group Product_Code

YM02/01/2024B
XM04/01/2024A
XN06/01/2024C
YN08/01/2024B
Final Result:

Product_Code Amount_Date

A1
B2
C1

Case 2: User selects Date range (01/01/2024 - 07/01/2024):

Intermediate Table (Max(Date) by Type and Group):

Type Group Max_Date_By_Type_Group Product_Code

YM02/01/2024B
XM04/01/2024A
XN06/01/2024C
YN07/01/2024A
Final Result:

Product_Code Amount_Date

A2
B1
C1

Question:

How can I implement this in Power BI? Specifically, I need help with:

  1. Creating the intermediate table (Max(Date) by Type and Group).
  2. Calculating the number of days matching Max(Date) for each Product_Code.

Any suggestions or DAX solutions would be greatly appreciated!

1 ACCEPTED SOLUTION
rajendraongole1
Super User
Super User

Hi @chuongnq  - We need to calculate the maximum date for each combination of Type and Group within the selected date range.

 

create a calculated table for typegroup as below with summarize function : 

 

MaxDatesByTypeGroup =
ADDCOLUMNS(
    SUMMARIZE(
        FILTER(
            ProdDate,
            ProdDate[Date] >= MIN(ProdDate[Date]) &&
            ProdDate[Date] <= MAX(ProdDate[Date])
        ),
        ProdDate[Type],
        ProdDate[Group]
    ),
    "Max_Date_By_Type_Group", CALCULATE(MAX(ProdDate[Date]))
)

 

rajendraongole1_1-1736486521800.png

 

 

 

To retrieve the corresponding Product_Code for the calculated Max_Date_By_Type_Group

Product_Code =
CALCULATE(
    MAX(ProdDate[Product_Code]),
    FILTER(
        ProdDate,
        ProdDate[Type] = EARLIER(MaxDatesByTypeGroup[Type]) &&
        ProdDate[Group] = EARLIER(MaxDatesByTypeGroup[Group]) &&
        ProdDate[Date] = EARLIER(MaxDatesByTypeGroup[Max_Date_By_Type_Group])
    )
)
rajendraongole1_2-1736486694676.png

 

 

Now, create a measure to calculate the number of days matching Max_Date_By_Type_Group for each Product_Code:

Amount_Date =
SUMX(
    FILTER(
        Proddate,
        Proddate[Product_Code] IN VALUES(MaxDatesByTypeGroup[Product_Code]) &&
        Proddate[Date] IN VALUES(MaxDatesByTypeGroup[Max_Date_By_Type_Group])
    ),
    1
)
rajendraongole1_3-1736486762244.png

 

output:

rajendraongole1_5-1736486899227.png

 

 

Hope this helps.


 

 

 





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

Proud to be a Super User!





View solution in original post

1 REPLY 1
rajendraongole1
Super User
Super User

Hi @chuongnq  - We need to calculate the maximum date for each combination of Type and Group within the selected date range.

 

create a calculated table for typegroup as below with summarize function : 

 

MaxDatesByTypeGroup =
ADDCOLUMNS(
    SUMMARIZE(
        FILTER(
            ProdDate,
            ProdDate[Date] >= MIN(ProdDate[Date]) &&
            ProdDate[Date] <= MAX(ProdDate[Date])
        ),
        ProdDate[Type],
        ProdDate[Group]
    ),
    "Max_Date_By_Type_Group", CALCULATE(MAX(ProdDate[Date]))
)

 

rajendraongole1_1-1736486521800.png

 

 

 

To retrieve the corresponding Product_Code for the calculated Max_Date_By_Type_Group

Product_Code =
CALCULATE(
    MAX(ProdDate[Product_Code]),
    FILTER(
        ProdDate,
        ProdDate[Type] = EARLIER(MaxDatesByTypeGroup[Type]) &&
        ProdDate[Group] = EARLIER(MaxDatesByTypeGroup[Group]) &&
        ProdDate[Date] = EARLIER(MaxDatesByTypeGroup[Max_Date_By_Type_Group])
    )
)
rajendraongole1_2-1736486694676.png

 

 

Now, create a measure to calculate the number of days matching Max_Date_By_Type_Group for each Product_Code:

Amount_Date =
SUMX(
    FILTER(
        Proddate,
        Proddate[Product_Code] IN VALUES(MaxDatesByTypeGroup[Product_Code]) &&
        Proddate[Date] IN VALUES(MaxDatesByTypeGroup[Max_Date_By_Type_Group])
    ),
    1
)
rajendraongole1_3-1736486762244.png

 

output:

rajendraongole1_5-1736486899227.png

 

 

Hope this helps.


 

 

 





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

Proud to be a Super User!





Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.