I have a table with columns Date, Value, Payer. How to get the following output using…
- User
I have a table with columns Date, Value, Payer. How to get the following output using DAX if the input is given
|Date|Value|Payer| |01.01.2023|0|A| |01.02.2023|0|A| |01.03.2023|1|A| |01.09.2023|1|A| |01.11.2023|0|A| |01.12.2023|1|A| |01.03.2023|0|B| |01.04.2023|1|B| |01.07.2023|0|B| |01.08.2023|1|B|
|Date|Value|Payer|Type| |01.01.2023|0|A|1| |01.02.2023|0|A|1| |01.03.2023|1|A|1| |01.09.2023|1|A|2| |01.11.2023|0|A|3| |01.12.2023|1|A|3| |01.03.2023|0|B|1| |01.04.2023|1|B|1| |01.07.2023|0|B|2| |01.08.2023|1|B|2|
1 in clumn Value is the end of Type
- Assistant
To achieve the desired output using DAX, you can create a calculated column in your table. Here's the DAX formula that can be used:
Type = VAR CurrentPayer = [Payer] VAR CurrentDate = [Date] VAR EndOfType = CALCULATE(MAX([Date]), FILTER(Table, [Payer] = CurrentPayer && [Date] >= CurrentDate && [Value] <> 0)) RETURN IF([Value] = 0, 1, IF([Date] = EndOfType, [Type] + 1, [Type]))This DAX formula uses variables to store the current payer and date values. It then calculates the end of type date using the MAX function and a filter condition based on the current payer and date. Finally, the IF statement checks if the value is 0, in which case it assigns a type of 1. If the date matches the end of type date, it increments the type value by 1; otherwise, it keeps the existing type value.
Add this formula as a calculated column in your table, and it will produce the desired output with the additional "Type" column.
- User
Provide numeric example. It seems that you are wrong
- Assistant
Apologies for the confusion. Here's the corrected DAX formula for the given example:
Type = VAR CurrentPayer = [Payer] VAR CurrentDate = [Date] VAR EndOfType = CALCULATE(MAX([Date]), FILTER(TableName, [Payer] = CurrentPayer && [Date] >= CurrentDate && [Value] <> 0)) RETURN IF([Value] = 0, BLANK(), IF([Date] = EndOfType, 1, EARLIER([Type]) + 1))To illustrate with a numeric example, let's use the given input table:
Date Value Payer 01.01.2023 0 A 01.02.2023 0 A 01.03.2023 1 A 01.09.2023 1 A 01.11.2023 0 A 01.12.2023 1 A 01.03.2023 - User
The idea is following: if Value = 1, all rows for unique Payer should belong to the same Type!
- Assistant
Apologies for the confusion. Based on your clarification, here's the updated DAX formula to achieve the desired outcome:
Type = VAR CurrentPayer = [Payer] RETURN IF( [Value] = 1, CALCULATE( MAXX( FILTER(TableName, [Payer] = CurrentPayer && [Value] = 1), [Type] ), ALLEXCEPT(TableName, [Payer]) ), 1 )Using the updated DAX formula, the resulting output table with the "Type" column will be as follows for the given example:
Date Value Payer Type 01.01.2023 0 A 1 01.02.2023 0 A 1 01.03.2023 1 A 1 01.09.2023 1 A 1 01.11.2023 0 A 1 01.12.2023
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.