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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
n8izzle
Frequent Visitor

Selected Column Based on Slicer

Hello all,

I am trying to create a dynamic column that will house a transaction date and will vary dependant on a slicer (SelectedAttribute[Attribute]). Ideally, I want to select "Order" on the slicer which will then adjust the column values to match with the "OrderDate" column. Then, if the user selects "Invoice" on the slicer, the columns date would change and match with the "InvoiceDate" column. I have another similar column that will pull the Amount based on same slicer which seems to be working using similar code as shown below.

 

Below is an example of what the dataset looks like:

 

InvoiceDateOrderDateInvoiceAmountOrderAmountSelectedDate
12/15/202212/01/202213.5026.50 
12/12/202204/01/202228.5053.50 
 12/15/2022 18.75 

 

Code I tried but the date always goes to the default "InvoiceDate":

 

 

SelectedDate = IF(
                    SELECTEDVALUE(SelectedAttribute[Attribute])="Order", DATEVALUE(ItemDetail[OrderDate]),
                    IF(SELECTEDVALUE(SelectedAttribute[Attribute])="Invoice",DATEVALUE(ItemDetail[InvoiceDate]), 
                    DATEVALUE(ItemDetail[InvoiceDate])))

 

 

 

Thank you for your help and support!

1 ACCEPTED SOLUTION

1. Create new calendar table

It holds all the date(s) from your data.

dim_calendar = CALENDAR(MIN(FIRSTDATE('Sample'[InvoiceDate]);FIRSTDATE('Sample'[OrderDate]));MAX(LASTDATE('Sample'[InvoiceDate]);LASTDATE('Sample'[OrderDate]))) 

2. Create relationship to OrderDate and InvoiceDate

 Note that Active relations is between Date and OrderDate and Inactive between Date and InvoiceDate.

bolfri_0-1671573998475.png

3. Create another table "Parameter" that holds an column with Invoice/Order value

You can use this DAX code or create a table using copy and pase from excel or something

Parameter = UNION( ROW("Type";"Invoice"); ROW("Type";"Order") )

4. Create a measure in this table that holds an information abour user pick

It means: show me what's picked in Parameter[Type] (if it's one value). If all of them or none are selected the default Type will be Order. It's recommended to use filter with single selection to avoid using exceptions.

Selected Type = SELECTEDVALUE(Parameter[Type];"Order")

 

Behaviour on this measure:

bolfri_1-1671574440834.pngbolfri_2-1671574449780.pngbolfri_3-1671574457368.pngbolfri_4-1671574465064.png
So change it to single selection 🙂

 

5. Create a measure based on slicer

This measure should contains an information about what to calculate in when Order/Invoice is selected.

Measure based on slicer =
SWITCH([Selected Type];
"Invoice";SUM('Sample'[InvoiceAmount]);
"Order";SUM('Sample'[OrderAmount])
)

 

You can see it in action right away in a card viz:

bolfri_6-1671574746291.pngbolfri_7-1671574756588.png

6. Create a measure to stick to correct date

Depending on user selection you are activating correct relationship.

Measure to date =
SWITCH([Selected Type];
"Invoice";CALCULATE([Measure based on slicer];USERELATIONSHIP(dim_calendar[Date];'Sample'[InvoiceDate]));
"Order";CALCULATE([Measure based on slicer];USERELATIONSHIP(dim_calendar[Date];'Sample'[OrderDate]))
)

7. You can viz it 🙂

REMEMBER TO USE A DATE FROM DIM_CALENDAR TABLE - not Order/Invoice Date

I have also created a dynamic title measure to help you understand whats goin on:

Dynamic title = "Sum of " & [Selected Type] & " amount by " & [Selected Type] & " date"

bolfri_8-1671575151858.pngbolfri_9-1671575159099.png




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

Proud to be a Super User!




View solution in original post

3 REPLIES 3
bolfri
Super User
Super User

Try this one.

1. Create new table

Solution = 
UNION(
    SELECTCOLUMNS(FILTER('Sample';NOT(ISBLANK('Sample'[InvoiceDate])));"Date";'Sample'[InvoiceDate];"Amount";'Sample'[InvoiceAmount];"Type";"Invoice");
    SELECTCOLUMNS('Sample';"Date";'Sample'[OrderDate];"Amount";'Sample'[OrderAmount];"Type";"Order")
)

2. Create new measure

Amount Value = SUM(Solution[Amount])

3. Viz it

bolfri_0-1671571309050.png

 





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

Proud to be a Super User!




Hi @bolfri , Thank you for your quick help! I tried this solution and it does work accordingly. However, I have many other columns on the main table and would like to try and keep it all on the same table.

 

Do you know if this is possible? Thanks again!

1. Create new calendar table

It holds all the date(s) from your data.

dim_calendar = CALENDAR(MIN(FIRSTDATE('Sample'[InvoiceDate]);FIRSTDATE('Sample'[OrderDate]));MAX(LASTDATE('Sample'[InvoiceDate]);LASTDATE('Sample'[OrderDate]))) 

2. Create relationship to OrderDate and InvoiceDate

 Note that Active relations is between Date and OrderDate and Inactive between Date and InvoiceDate.

bolfri_0-1671573998475.png

3. Create another table "Parameter" that holds an column with Invoice/Order value

You can use this DAX code or create a table using copy and pase from excel or something

Parameter = UNION( ROW("Type";"Invoice"); ROW("Type";"Order") )

4. Create a measure in this table that holds an information abour user pick

It means: show me what's picked in Parameter[Type] (if it's one value). If all of them or none are selected the default Type will be Order. It's recommended to use filter with single selection to avoid using exceptions.

Selected Type = SELECTEDVALUE(Parameter[Type];"Order")

 

Behaviour on this measure:

bolfri_1-1671574440834.pngbolfri_2-1671574449780.pngbolfri_3-1671574457368.pngbolfri_4-1671574465064.png
So change it to single selection 🙂

 

5. Create a measure based on slicer

This measure should contains an information about what to calculate in when Order/Invoice is selected.

Measure based on slicer =
SWITCH([Selected Type];
"Invoice";SUM('Sample'[InvoiceAmount]);
"Order";SUM('Sample'[OrderAmount])
)

 

You can see it in action right away in a card viz:

bolfri_6-1671574746291.pngbolfri_7-1671574756588.png

6. Create a measure to stick to correct date

Depending on user selection you are activating correct relationship.

Measure to date =
SWITCH([Selected Type];
"Invoice";CALCULATE([Measure based on slicer];USERELATIONSHIP(dim_calendar[Date];'Sample'[InvoiceDate]));
"Order";CALCULATE([Measure based on slicer];USERELATIONSHIP(dim_calendar[Date];'Sample'[OrderDate]))
)

7. You can viz it 🙂

REMEMBER TO USE A DATE FROM DIM_CALENDAR TABLE - not Order/Invoice Date

I have also created a dynamic title measure to help you understand whats goin on:

Dynamic title = "Sum of " & [Selected Type] & " amount by " & [Selected Type] & " date"

bolfri_8-1671575151858.pngbolfri_9-1671575159099.png




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

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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