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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
supicek
Regular Visitor

replace a calculated column with a measure

Hi 
I am quite new in PBI, but in past I have made a few simple models. Now I have to use data from direct query (therefore I cannot use calculated columns anymore). Here is my problem:

Original solution using calculated columns
Type      InvAmnt   ccTotalPerType      ccPercent
A           30         50               0.6 (30/50)
A           20         50               0.4 (20/50)  
B           10         35               0.28 (10/35)

B           15         35               0.42 (15/35)  

I think usage of calculated columns in this context is correct, but due to DirectQuery Model I cannot use it. Can I somehow calculate the ccPercent by using measures? I was thinking of creating a measure for each type, but I cannot predict all types that will appear in the data source. Below is the DAX formula used for calculated colum, but I cannot figure out how to derive Type for each row in case I will use a measure.

ccTotalPerType   =
VAR myType=Source[Type]
Return
CALCULATE(Sum([InvAmnt]), FILTER('Source',Source[Type]=myType))

 

thank you for your advice

1 ACCEPTED SOLUTION
supicek
Regular Visitor

I think I found the function  I was looking for:
SELECTEDVALUE(Soure[Type]) 
then I can migrate calculated column to measure and the measure will look like

meTotalPerType   =
CALCULATE(Sum([InvAmnt]), FILTER('Source',Source[Type]=SelectedValue(Source[Type]))




View solution in original post

3 REPLIES 3
supicek
Regular Visitor

thank you, but this solution will not work if I do not use Type in visuals.
I received a brilliant and simple solution from @smpa01 
measure = DIVIDE(sum(tbl[invAmt]),calculate(sum(tbl[invAmt]),allexcept(tbl,tbl[Type])))
which perfectly works for me.
thank  both of you

supicek

PhilipTreacy
Super User
Super User

Hi @supicek 

 

You'll need to use ALL() inside FILTER() to get all the rows in the table for that particular type

 

meTotalPerType = 
CALCULATE(Sum([InvAmount]), FILTER(ALL('Source'),'Source'[Type]=SelectedValue('Source'[Type])))

 

metotal.png

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


supicek
Regular Visitor

I think I found the function  I was looking for:
SELECTEDVALUE(Soure[Type]) 
then I can migrate calculated column to measure and the measure will look like

meTotalPerType   =
CALCULATE(Sum([InvAmnt]), FILTER('Source',Source[Type]=SelectedValue(Source[Type]))




Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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