Power Bi: Get Column Name with Maximum and Minimum other column value - powerbi

I have a sheet containing employees leave data-
Staff Leave Taken Month
A 19 April
A 3 May
A 3 June
B 2 April
B 1 May
B 0 June
C 0 April
C 0 May
C 1 June
I want to calculate the Employee whose has taken maximum no. of leaves and name of employee whose has taken minimum no. of leaves.
Here Employee with max. leaves is A and with min. is C.
I am having trouble in getting the maximum no. of leaves.
X = MAX( SUMX ( SUMMARIZE ( Table1, Table1[STAFF], Table1[Leaves] ), [Leaves] ))
But it is showing some error.
I tried to group by staff name then also it is not working out.

You can first create a summary table with the following DAX:
Summary = SUMMARIZE(Table1, Table1[Staff], "Leaves", SUM(Table1[Leave Taken]))
Then you can use the following DAX measure to get the Max / Min name:
Max Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MAX(Summary[Leaves])
)
)
-
Min Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MIN(Summary[Leaves])
)
)

Related

Power BI - Get date when sum is 0

I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.

DAX PowerBI: Replaced blank values with zero and issue with chart

I tried to create report in Power BI with sales month by month for last 20 months, when sales is blank I want to see month with 0 value.
I decided to change Blank() values with zero adding a 0 at the end of calculation.
It works great, however I have an issue with limitaton date hierarchy, because now my chart contains a lot of months without value (first value begins in 2017, date hierarchy first value begins in 2000).
Test:=
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + 0
Instead of a plain 0, you could add an IF to specify to only add that after the first value. Something like this:
Test:=
VAR FirstDate = CALCULATE ( MIN ( date ), ALL( Dates ), quantity > 0 )
RETURN
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + IF( date > FirstDate, 0 )
If the condition is false, the IF returns a blank and it shouldn't show up.

Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. My table has dates that are the 1st day of every month only .. no other days in the month. I'm asking the question about DAX; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well.
I have been able to get a measure to work using something like this..
CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))
But I'd like to be a new column instead.
Assuming the table looks something like this..
A B C D AMT
6 BAC456 5/1/2019 TEST 25
2 EPS123 4/1/2019 TEST 45
2 EPS123 3/1/2019 TEST 65
6 BAC456 4/1/2019 TEST 43
6 BAC456 4/1/2019 TEST 88
7 GRE123 4/1/2019 TEST 90
9 BAC456 4/1/2019 TEST 43
I'd like to have another column in this table where the first row would be:
A B C D AMT NEWCOL
6 BAC456 5/1/2019 TEST 25 131
Second row would be:
A B C D AMT NEWCOL
2 EPS123 4/1/2019 TEST 45 65
etc..
In cases where the month column is the first month in the entire table NEWCOL would be 0
To get 131, I'm assuming that you are requiring a match on both columns A and B.
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
PREVIOUSMONTH ( Table1[C] )
)
This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C. Note that this returns a blank for rows that don't have a previous month. If you'd prefer 0 then add + 0 after the last closing parenthesis.
If PREVIOUSMONTH doesn't work, then try this:
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)
For date = 5/1/2019, EOMONTH ( date, -2 ) returns 3/31/2019. Add one day to get 4/1/2019.
To achieve this easily in a calculated column, you need something like this before writing the final DAX.
Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)
And now you have these Month differences and Month numbers defined, you can write a DAX like this -
Amount - New Column =
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1
This makes my table to look something like,
I have used Var(Variables) in my formula to help you understand what is happening inside the formula.
Kindly accept the answer if it solves your problem.

Is there a way to create measure using variable to return last month Base size in power BI or DAX

I'm creating a report in Power BI, and want to return the last month Size.
I have a table with 4 columns named as Name, Size, Connections, Disconnections. The values on these columns are for the last 12 months. For example, Name column has A, B, C; Size column has 3608445, 2839945,874434; Connections column has 66875,85632,19237 and Disconnections column has 52658,61529 and 15832 values. These values are for the last 12 months. See screenshot below.
The code I used to created the expected table is
last_month_size =
VAR current_month =
MONTH ( TODAY () )
RETURN
CALCULATE (
[Size],
FILTER (
'Monthly Calendar_Lookup',
MONTH ( 'Monthly Calendar_Lookup'[Dates] ) = current_month - 1
)
)
I want to create a measure that will return last month Size column but the Connections and Disconnections remains the same. For example, the Size value changes while the connections and disconnections values remains the last 12 month values.
I find it difficult because the columns are on the same table.
I have researched about the question I have posted and I have found a solution.
This solution to the problem is creating measures and not using variables.
First, I created a measure called Total Size
Total Size = Sum ( Tablename [Size] )
Then, created another measure called prev_month size using DATEADD function with number_of_intervals as 0
prev_month size = CALCULATE ( [Total Size], DATEADD ('Monthly Calendar_Lookup'[Dates], 0, MONTH ) )
Next, I created measures of total connections and total disconnections
Total Connections = Sum ( Tablename [Connections] )
Total Disconnections = Sum ( Tablename [Disconnections] )
Also, I created two measures of rolling 12 months Connections and Disconnections each.
Rolling_Connections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Connections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Rolling_Disconnections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Disconnections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Drag the Name, prev_month size, Rolling_Connections_12_months, and Rolling_Disconnections_12_months on the canvas as a table visualization.
Then finally, I drag a relative Date slicer and set it as Last 1 Month.
This produces the expected results

Number of Persons above the Mean

I have this simple data set from Excel:
Date Person Amount
Jan-18 jason 1
Jan-18 fred 2
Jan-18 george 3
Feb-18 jason 10
Feb-18 fred 12
Feb-18 george 15
Feb-18 jim 25
I added two measures:
Amount = SUM( Data[Amount] )
and
Average Amount per Person =
AVERAGEX(
VALUES( Data[Person]),
[Amount]
)
This works as I expect and is dynamic when I select a specific Date:
What I now want is "Number of Persons Above Average" - so in the screenshot only Jim is above 15.50 so the measure should return 1.
My attempt at this measure is this:
Number of Persons Above Average =
CALCULATE(
DISTINCTCOUNT( Data[Person] ),
FILTER(
Data,
SUM( Data[Amount] ) >= [Average Amount per Person]
)
)
As you can see below it just returns the number of persons displayed - in this case 4
How do I amend the above measure to the correct DAX ?
I like to use variables in situations like these:
Number of Persons Above Average =
VAR AveragePerPerson = [Average Amount per Person]
RETURN
CALCULATE ( DISTINCTCOUNT ( Data[Person] ),
Data[Amount] >= AveragePerPerson )
This way you don't have to worry about how the average measure will be computed inside of the CALCULATE and you don't have to use a FILTER function.