Calculate difference in cost based on combined query results - powerbi

i have a table in Power BI that is the result of a combined query (3 tables), which means i have a column with 3 different project IDs (4010 = current project, 3844 = previous month's project, 3653 = baseline)
i have another column called "actual cost" and what i would like to do is add a calculated column that calculates for each row the difference between the actual cost in ID 4010 vs ID 3844, based on the denominator activity ID or name.
how would i go about doing this?

Personally I would prefer a Measure in such situations. A Calculated Column is less flexible and more overhead. However, since that it was you asked for:
=
VAR ThisActivityID = Table1[activity ID]
VAR ActualCost4010 =
CALCULATE(
SUM( Table1[actual cost] ),
FILTER(
ALL( Table1 ),
Table1[activity ID] = ThisActivityID
&& Table1[project ID] = 4010
)
)
VAR ActualCost3844 =
CALCULATE(
SUM( Table1[actual cost] ),
FILTER(
ALL( Table1 ),
Table1[activity ID] = ThisActivityID
&& Table1[project ID] = 3844
)
)
RETURN
ActualCost4010 - ActualCost3844

Related

Is there any DAX Measure that Count rows that have an ID matching another row, but a different value in the column

I have a table with ID and with different statuses but some are not having both. I need create measure that will give the count for all the ID's that are having different statues.
I have tried below Dax functions but not giving the expected results.
Link-EC-Count =
VAR count_Donor =
CALCULATE (
DISTINCTCOUNT ( MagentaBuilt_Linked[Microwave IQ Link ID] ),
FILTER (
ALLEXCEPT ( MagentaBuilt_Linked, MagentaBuilt_Linked[Microwave IQ Link ID] ),
MagentaBuilt_Linked[Site_Nature] = "Donor"
),
FILTER (
MagentaBuilt_Linked,
MagentaBuilt_Linked[Primary AAV Vendor] = "T-Mobile"
)
)
VAR count_Recepient =
CALCULATE (
DISTINCTCOUNT ( MagentaBuilt_Linked[Microwave IQ Link ID] ),
FILTER (
ALLEXCEPT ( MagentaBuilt_Linked, MagentaBuilt_Linked[Microwave IQ Link ID] ),
MagentaBuilt_Linked[Site_Nature] = "Recipient"
),
FILTER (
MagentaBuilt_Linked,
MagentaBuilt_Linked[Primary AAV Vendor] = "T-Mobile"
)
)
RETURN
IF ( count_Donor > 0 && count_Recepient > 0, 1, 0 )
Here is the sample data looks like
Anybody have any ideas?
Here's a calculated column formula for "Eligible Row?" that works:
Eligible Row? =
IF(
CALCULATE(
COUNTROWS('Table'),
FILTER('Table',
'Table'[Affiliate ID]=EARLIER('Table'[Affiliate ID]) &&
'Table'[Achievement Type] <> EARLIER('Table'[Achievement Type])))>0,
"Yes","No")
The idea is to filter the table for rows when the Affiliate ID matches, then filter for rows where the Achievement is different, then count the rows.
Here are formulas for my two intermediate calculated columns:
Count ID =
CALCULATE(
COUNTROWS('Table'),
FILTER('Table',
'Table'[Affiliate ID]=EARLIER('Table'[Affiliate ID])))
Count Other Achievements =
CALCULATE(
COUNTROWS('Table'),
FILTER('Table',
'Table'[Affiliate ID]=EARLIER('Table'[Affiliate ID]) &&
'Table'[Achievement Type] <> EARLIER('Table'[Achievement Type])))
I think This is what you need. Try this code as measure:
Count ID =
VAR TblSummary = ADDCOLUMNS(
SUMMARIZE('Sheet 1','Sheet 1'[Affiliate Id],'Sheet 1'[Achievement Type]),
"UniqueCount",CALCULATE(COUNT('Sheet 1'[Affiliate Id]),ALLEXCEPT('Sheet 1','Sheet 1'[Affiliate Id]))
)
RETURN
SUMX(TblSummary,IF([UniqueCount]>1,[UniqueCount]))
If we test it on a visual:

Measure to calculate most repeated value in a column

I'm working with a Soccer League Database with a table like this:
Soccer League Table
I would like to create a measure to calculate the most repeated Division that every team has played in all the seasons.
For example: For Team A it will be 1 and for Team B 3
Thank you!
Regards
Assuming a table named Table1, this measure:
MostRepeatedDivision :=
VAR T1 =
SUMMARIZE( Table1, Table1[DIVISION], "Count", COUNTROWS( Table1 ) )
VAR MostRepeated =
MAXX( T1, [Count] )
RETURN
MAXX( T1, IF( [Count] = MostRepeated, Table1[DIVISION] ) )
You can achieve this with the following expressions:
Create a calculated table counting the frequency of each division
Frequency =
SUMMARIZE(
Soccer,
Soccer[Team], Soccer[Divison],
"Number", Count(Soccer[Divison])
)
Add a calculated column that ranks the frequencies per team
Rank =
RANKX(
FILTER(
ALL('Frequency'),
Frequency[Team] = EARLIER(Frequency[Team])
),
'Frequency'[Number]
)
The resulting calculated table showing teams and top divisions
Top Division =
SELECTCOLUMNS(
FILTER(
Frequency,
Frequency[Rank] = 1
),
"Team", Frequency[Team],
"Division", Frequency[Divison]
)

Hard coded filter in PowerBi measure

I have a measure (Users_1) that calculates number of rows between specific dates that have the parameter is_sql = 0.
This measure is used in a table alongside with other measures.
I have 5 more filters on the page that that should affect this specific measure and so I can not use All(users).
One of the filters on this page is "is_sql". And when is_sql = 1 every measure except for measure (Users_1) should change to correspondig value. Measure (Users_1) shold stay the same.
Now when I chose is_sql = 1 the measure (Users_1) is blank.
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
FILTER(
KEEPFILTERS('users'),
'users'[date (days)] <= MAX( 'Calendar'[Date] )
&&'users'[date (days)] >= MIN( 'Calendar'[Date] )
&&'users'[is_SQL] = 0
)
)
You will want to avoid using KEEPFILTERS on any filters you want to allow to look outside of the filter context:
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
'users'[date (days)] <= MAX( 'Calendar'[Date] ),
'users'[date (days)] >= MIN( 'Calendar'[Date] ),
'users'[is_SQL] = 0
)

PowerBI DAX for Finding Last Record

Question
What is an efficient way to create a calculated column finding the last value of my DATE column, using the ModifiedOn column, per ID? I don't want the MAX date, just the last record (even if the last record is the minimum). Also, my table is a calculated column.
Example Table
ID
DATE
ModifiedOn
A
2/4/2020
1/16/2019
A
2/5/2020
1/17/2019
B
3/2/2020
2/7/2020
B
3/3/2020
2/8/2020
B
3/1/2020
2/9/2020
Current Formula
LastRecord =
VAR Max_Date =
CALCULATE (
MAX ( 'Table1'[ModifiedOn] ),
ALLEXCEPT ( 'Table1', 'Table1'[ID] )
)
RETURN
IF (
Table1[ModifiedOn] = Max_Date,
Table1[DATE]
)
Current Results
But using the formula I get a calculated column that looks like this:
I keep getting blanks where they should be filled with the LAST recorded date of that ID.
Use the following dax formula to create the expected column:
Column =
VAR __id = 'Table'[ID]
VAR __lastMod =
CALCULATE(
MAX( 'Table'[ModifiedOn] ),
FILTER( 'Table', 'Table'[ID] = __id )
)
VAR __lastDate =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER( 'Table', 'Table'[ID] = __id && 'Table'[ModifiedOn] = __lastMod )
)
Return __lastDate

Pareto (80/20) Segmentation in Power BI

I created 80/20 segmentation in my Power BI data model and I got what I wanted (see the table below).
Now I want to calculate new Name column with the next logic: if Cumulative % <=80% show value from the "Customer Name" column, otherwise show "Other" (the result will be the column Name as in the table below).
I tried with this calculated column but it doesn't work (the result isn't correct, it's always "Other"):
80/20 Name = IF([Cumulative Percen.] <= 0.8, SalesReport[Names], "Other")
Note: "Cumulative Percen." is calculated measure.
How can I do this?
In the next step, I'll use a pie chart to show this segmentation where all customers with small cumulative transactions will be categorized as Other.
The calculated measures that I used:
Customer Rank by Transaction =
IF (
HASONEVALUE ( SalesReport[CustName] ),
RANKX (
ALLSELECTED ( SalesReport[CustName] ),
CALCULATE ( [# of Transactions] ),
,
DESC,
DENSE
)
)
Customer Cumulative Transaction =
VAR CurrentCustimerRank = [Customer Rank by Transaction]
RETURN
SUMX (
FILTER (
ALLSELECTED ( SalesReport[CustName] ),
CALCULATE ( [Customer Rank by Transaction] ) <= CurrentCustimerRank
),
CALCULATE ( DISTINCTCOUNT ( SalesReport[CustID] ) )
)
Customer Cumulative Transaction Percen. =
[Customer Cumulative Transaction]
/ CALCULATE (
DISTINCTCOUNT ( SalesReport[CustID] ),
ALLSELECTED ( SalesReport[CustName] )
)