I have Table1 with columns Customer, SKU, Date in Power BI. Date column contains dates.…
- User
I have Table1 with columns Customer, SKU, Date in Power BI. Date column contains dates. How to create the table with Customer, SKU and dates not present in column Date in Table1 among all dates in 2022 and 2023 for each combination of Customer and SKU? Use DAX
- Assistant
To create a table containing all unique combinations of Customer, SKU, and dates not present in the Date column of Table1, you can follow these steps using DAX in Power BI:
-
Create a new calculated table:
NewTable = GENERATE( SELECTCOLUMNS(Table1, "Customer", [Customer], "SKU", [SKU]), SUMMARIZE( FILTER( CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31)), NOT CONTAINS(Table1, [Customer], [SKU], [Date]) ), [Date] ) )The
GENERATEfunction is used to create rows for all unique combinations of Customer and SKU. TheSELECTCOLUMNSfunction selects the "Customer" and "SKU" columns from Table1. TheSUMMARIZEfunction creates a summarized table with the "Date" column from the filtered CALENDAR table. -
Remove any duplicate rows in the NewTable:
NewTable = DISTINCT(NewTable)The function filters out any duplicate rows in the NewTable.
-
- User
Customer_SKU_Date (non promo days) = GENERATE( SELECTCOLUMNS('Customer_SKU_Date (promo days)', "Customer", [Customer], "SKU", [SKU]), SUMMARIZE( FILTER( CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31)), NOT CONTAINS('Customer_SKU_Date (promo days)', [Customer], [SKU], [Date]) ), [Date] ) ) The error is The number of arguments is invalid. Function CONTAINS must have a value for each specified column reference. Fix that
- Assistant
Apologies for the error in the previous response. The DAX function should be modified to correctly check for the absence of values in the 'Customer_SKU_Date (promo days)' table. Here is the updated code:
Customer_SKU_Date (non promo days) = GENERATE( SELECTCOLUMNS('Customer_SKU_Date (promo days)', "Customer", [Customer], "SKU", [SKU]), SUMMARIZE( FILTER( CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31)), NOT CONTAINS('Customer_SKU_Date (promo days)', [Customer], [SKU], [Date]) ), [Date] ), "Flag", 0 )The
'Customer_SKU_Date (non promo days)'table is generated using theGENERATEfunction. TheSELECTCOLUMNSfunction selects the "Customer" and "SKU" columns from the'Customer_SKU_Date (promo days)'table.The
FILTERfunction inside theSUMMARIZEfunction is used to filter theCALENDARtable for dates that are not present in the'Customer_SKU_Date (promo days)'table for each combination of Customer and SKU. - User
The error "The number of arguments is invalid. Function CONTAINS must have a value for each specified column reference." is available. Fix that
- Assistant
I apologize for the confusion. It seems that the CONTAINS function is not appropriate for this scenario. Instead, you can use a combination of the SUMMARIZE and EXCEPT functions to achieve the desired result. Here's an updated version of the DAX expression:
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date', 'Customer_SKU_Date'[Customer], 'Customer_SKU_Date'[SKU], 'Customer_SKU_Date'[Date]) RETURN GENERATE( SELECTCOLUMNS('Customer_SKU_Date', "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), ExistingDates ) )In this expression, the variable "ExistingDates" is defined using the SUMMARIZE function. It summarizes the dates from the 'Customer_SKU_Date' table based on the 'Customer', 'SKU', and 'Date' columns.
Then, the GENERATE function is used to generate rows for all combinations of 'Customer' and 'SKU' in the 'Customer_SKU_Date' table. The SELECTCOLUMNS function selects the 'Customer' and 'SKU' columns.
- User
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date]) RETURN GENERATE( SELECTCOLUMNS('Customer_SKU_Date (promo days)', "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), ExistingDates ) )
The error is "The VALUES function expects a column reference expression or a table reference expression for argument '1'."
- Assistant
Apologies for the confusion. The error is occurring because the VALUES function expects a column reference expression or table reference expression as its argument.
To fix the error and achieve the desired result, please use the following updated DAX expression:
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date] ) RETURN GENERATE( SELECTCOLUMNS(ExistingDates, "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), SELECTCOLUMNS(ExistingDates, "Date", [Date]) ) )In the updated expression, the SELECTCOLUMNS function is used to select the 'Customer' and 'SKU' columns from the 'ExistingDates' variable, which contains the summarized dates from the 'Customer_SKU_Date (promo days)' table.
- User
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date]) RETURN GENERATE( SELECTCOLUMNS(ExistingDates, "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), SELECTCOLUMNS(ExistingDates, “Date”, [Date]) ) )
The error is The following syntax error occurred during parsing: Invalid token, Line 12, Offset 42, “.
- Assistant
Apologies for the error. It seems there's a syntax mistake in the code. In the updated code, the double quotes are incorrectly formatted. Please use the straight double quotes instead of the curly double quotes around the "Date" column name. Here's the corrected code:
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date] ) RETURN GENERATE( SELECTCOLUMNS(ExistingDates, "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), SELECTCOLUMNS(ExistingDates, "Date", [Date]) ) )The SELECTCOLUMNS function is used to select the "Customer" and "SKU" columns from the ExistingDates variable. Similarly, the EXCEPT function is used to find the dates that are present in the CALENDAR function but not present in the "Date" column of ExistingDates.
Now, the Customer_SKU_Date (non promo days) table will contain unique combinations of "Customer," "SKU," and dates that are not present in the "Date" column of the Customer_SKU_Date (promo days) table but are present in the calendar from January 1, 2022, to December 31, 2023.
- User
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date]) RETURN GENERATE( SELECTCOLUMNS(ExistingDates, "Customer", [Customer], "SKU", [SKU]), EXCEPT( VALUES(CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31))), SELECTCOLUMNS(ExistingDates, "Date", [Date]) ) )
The error is "The VALUES function expects a column reference expression or a table reference expression for argument '1'."
- Assistant
Apologies for the confusion. The error is occurring because the VALUES function does not accept a table or a column reference as an argument. To address this error, we need to modify the syntax slightly. Here's the revised code:
Customer_SKU_Date (non promo days) = VAR ExistingDates = SUMMARIZE('Customer_SKU_Date (promo days)', 'Customer_SKU_Date (promo days)'[Customer], 'Customer_SKU_Date (promo days)'[SKU], 'Customer_SKU_Date (promo days)'[Date] ) RETURN GENERATE( SELECTCOLUMNS(ExistingDates, "Customer", [Customer], "SKU", [SKU]), EXCEPT( CALENDAR(DATE(2022, 1, 1), DATE(2023, 12, 31)), VALUES(ExistingDates[Date]) ) )In this revised code, we've made a couple of changes. First, we have removed the SELECTCOLUMNS function around ExistingDates while using the VALUES function. Second, we have directly used ExistingDates[Date] as the argument in the VALUES function.
Now, the Customer_SKU_Date (non promo days) table will contain unique combinations of "Customer," "SKU," and dates that are not present in the "Date" column of the Customer_SKU_Date (promo days) table but are present in the calendar from January 1, 2022, to December 31, 2023.