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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
chuongnq
Regular Visitor

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

    • Problem Statement:

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

hi
1) Use the following DAX formula to create a calculated table:

IntermediateTable = 
SUMMARIZE(
    FILTER(
        'Dataset A',
        'Dataset A'[Date] IN VALUES('DateTable'[Date])
    ),
    'Dataset A'[Type],
    'Dataset A'[Group],
    "Max_Date_By_Type_Group", MAX('Dataset A'[Date]),
    "Product_Code",
    FIRSTNONBLANK(
        FILTER(
            'Dataset A',
            'Dataset A'[Date] = MAX('Dataset A'[Date])
        ),
        'Dataset A'[Product_Code]
    )
)

 

2) Use this DAX measure to calculate the count of days matching Max_Date_By_Type_Group:

Amount_Date = 
CALCULATE(
    COUNTROWS('Dataset A'),
    'Dataset A'[Date] IN DISTINCT(IntermediateTable[Max_Date_By_Type_Group]),
    'Dataset A'[Product_Code] IN DISTINCT(IntermediateTable[Product_Code])
)

Visualize:

  • Add Product_Code and Amount_Date to a table visual.
  • The slicer on Date will dynamically filter results.

This ensures the correct Product_Code and Amount_Date based on the slicer selection.

View solution in original post

1 REPLY 1
rohit1991
Super User
Super User

hi
1) Use the following DAX formula to create a calculated table:

IntermediateTable = 
SUMMARIZE(
    FILTER(
        'Dataset A',
        'Dataset A'[Date] IN VALUES('DateTable'[Date])
    ),
    'Dataset A'[Type],
    'Dataset A'[Group],
    "Max_Date_By_Type_Group", MAX('Dataset A'[Date]),
    "Product_Code",
    FIRSTNONBLANK(
        FILTER(
            'Dataset A',
            'Dataset A'[Date] = MAX('Dataset A'[Date])
        ),
        'Dataset A'[Product_Code]
    )
)

 

2) Use this DAX measure to calculate the count of days matching Max_Date_By_Type_Group:

Amount_Date = 
CALCULATE(
    COUNTROWS('Dataset A'),
    'Dataset A'[Date] IN DISTINCT(IntermediateTable[Max_Date_By_Type_Group]),
    'Dataset A'[Product_Code] IN DISTINCT(IntermediateTable[Product_Code])
)

Visualize:

  • Add Product_Code and Amount_Date to a table visual.
  • The slicer on Date will dynamically filter results.

This ensures the correct Product_Code and Amount_Date based on the slicer selection.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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