Creating a calculated table by passing a measure or parameter - powerbi

I have a requirement where I have a data like this,
Date Name Age
1-1-2018 A 1
2-2-2018 B 1
3-3-2018 B 1
6-6-2018 C 2
7-7-2018 B 6
I am trying to give a slicer to the user to select the required number of months from the last month.
So to do that, I am using a calculated column like this:
Month Year = DATEDIFF((Table1[Date]), TODAY(), MONTH) + 1
So that changes the data to something like this:
Date Name Age MonthYear
1-1-2018 A 1 7
2-2-2018 B 1 6
3-3-2018 B 1 5
6-6-2018 C 2 2
7-7-2018 B 6 1
The user selects the Month Year from the Slicer.
For example, when he selects 2, I want to display the last 2 months records in the table.
Expected Output:
Date Name Age
6-6-2018 C 2
7-7-2018 B 6
This works for me if I hardcode it like this:
Calculated Table = CALCULATETABLE(Table1,
FILTER(Table1, OR(Table1[MonthYear] > 2, Table1[MonthYear] = 2)))
But it fails when I try to pass the value in the place of 2 dynamically through a measure using SelectedValue function.

Calculated columns and calculated tables cannot reference a slicer value since they are only computed when you load your data.
If you want to apply this filtering to a visual, I'd suggest creating a separate table for your slicer. For example, you could use Months = GENERATESERIES(1,12) and then rename the column Months as well.
Use the Months[Months] column for your slicer and then create a measure which references it to filter your table/matrix visual.
Filter = IF(SELECTEDVALUE(Months[Months]) >= MAX(Table1[Month Year]), 1, 0)
Then use that measure in your Visual level filters box:

Related

Return Slicer's Value (trade simulator)

I work with a single table (called sTradeSim) that I have created in PowerQuery. It has 3 columns (Fund1, Fund2, Fund3), each having values from -10 to 10, with an increment of 1.
I also have three separate slicers, each created using an option "Greater than or equal to". Each slicer is having a field assigned to it - Slicer 1 = Fund1, Slicer 2 = Fund2, Slicer 3 = Fund3. Below is a screenshot of Slicer 1.
Right next to these three slicers is a table with three rows. For each row, I would like to retrieve the value of the respective slicers. So the desired result would look like:
Row No 1 = -10.00 (the value of Slicer 1),
Row No 2 = -2.00 (the value of Slicer 2),
Row No 3 = 3.00 (the value of Slicer 3).
Unfortunately, DAX formula that I have developed is always returning 3.00 (the value of the third slicer).
I have tried to find a solution on the forum and combine my SWITCH formula with ALL, ALLEXCEPT, SELECTEDVALUE etc., but it seems like I'm missing something very basic.
mHV_Trades =
SWITCH(
MAX(FundTable[FundsRanked]),
1, MIN(sTradeSim[Fund1]),
2, MIN(sTradeSim[Fund2]),
3, MIN(sTradeSim[Fund3])
)
What you are trying to do doesn't work, because essentially when you place 1 filter on any column on the table, it will filter all the rows that have that value. So, when you apply a filter fund1 = -10 it will also filter the values for fund 2 and fund 3.
You have 2 options:
Create independent tables each with values from -10 to 10
Create a table with all the combinations of -10 to 10 values for every fund.
For your example with 3 funds this works quite nicely (the table has about 10k records), all the combinations of -10 to 10 (21) to the power of 3, the problem with this solution is that depending on the number of funds you have you will run out of space quite quickly.

Power Bi DAX: Divide Value and set it for each week

I have a target value of 20 for January but it is 20 for the month, i need to show this over each week. I have to divide the target by 4 if there are 4 weeks in a month and by 5 if there are 5 weeks in a month. It is as simple as that, i am using a line and clustered column chart to display my data, i need the target spread out into each week of the month. I also need another field to do this but im sure i can replicate your formula and make it applicable.
I have added a WeeksinMonth column that counts how many weeks are in a particular month e.g January has 5 weeks and February has 4 weeks. I need an IF statement that will divide the target and value by how many weeks in a month. e.g if month has 5 weeks then divide target and value by 5, if month has 4 weeks divide target and value by 4.
I have a calendar table with week values which i can used to put the divided target into and also the desired output i made in excel (See attached).
How will i go about this?
Desired Output
Calendar Table
enter code hereYou can create an extra column in your calendar table:
WeekMax =
var stOfMonth = Weeks[Start of Week].[Month]
var stOfYear = Weeks[Start of Week].[year]
return CALCULATE(max(Weeks[Weekinmonth]);FILTER(Weeks;Weeks[Start of Week].[Month] = stOfMonth && Weeks[Start of Week].[Year] = stOfYear))
Make sure you have a relation in your model between calendar and target date. Now you can use this number for that week/date to divide by.
You can now add an extra column in your target table:
WeeklyTarget = Targets[Target]/Related(Calendar[WeekMax])

I would like to create a measure in powerBI that is the standard deviation of distinct count by month

I would like to create a measure in powerBI that is the standard deviation of the distinct counts of IDs by month. i.e:
for this data set:
Month | ID
1 A
1 B
1 A
2 C
2 D
2 E
3 F
3 G
The Counts would be:
Month | Distinct Count
1 2
2 3
3 2
Standard Dev = 0.47
Thank you!!!!!
Below is an iteration of the code I tried that hasn't worked:
standard dev by month = STDEVX.P(values('Table'[Reporting Date By Month Year]), DISTINCTCOUNT('Table'[ID]))
Using the table you provided named as "Table" you can use the following measure to get the standard deviation.
Standard Deviation =
var grouped_table = SUMMARIZE('Table','Table'[Month],"Distinct Count",DISTINCTCOUNT('Table'[ID]))
var st_dev = STDEVX.P(grouped_table,[Distinct Count])
return(st_dev)
The measure uses variables to make the measure more easy to read. The first table generates the group of months and the respective distinct count of IDs. The second variables calculated the standard deviation of that table by distinct count measure.

Power BI DAX Compute Latest Value for filtered rows

I have data in a Power BI DirectQuery table that looks line this:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017
A 2 1/3/2017
A 3 1/4/2017
B 4 1/5/2017
B 5 1/6/2017
C 6 1/1/2017
C 7 1/2/2017
C 8 1/9/2017
In a table visual, I want to show only the latest rows for each name:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 3 1/4/2017
B 5 1/6/2017
C 8 1/9/2017
However, I am using a slicer to help filter our dates and I want the latest rows that are in the sliced data. For example, if the sliced date range is 1/1/2017 to 1/2/1017, inclusive, I want to show this:
Name Tx TxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017
C 7 1/2/2017
I've been trying to accomplish this by creating a measure with a DAX expression to compute the latest Date for each name, then adding a filter to only show those where LatestTxDate = TxDate, but it is not working:
LastTxDate =
MAXX(
GROUPBY(
Table,
[Name],
"Latest Tx Date",
MAXX(CURRENTGROUP(), Table[TxDate])
),
[Latest Tx Date]
)
The idea being that I calculate a new measure and then only make the row visible if the TxDate = LastTxDate.
Name Tx TxDate LastTxDate Other Columns
-------------------------------------------------------
A 1 1/1/2017 1/1/2017
A 2 1/3/2017 1/1/2017
A 3 1/4/2017 1/1/2017
B 4 1/5/2017
B 5 1/6/2017
C 6 1/1/2017 1/2/2017
C 7 1/2/2017 1/2/2017
C 8 1/9/2017 1/2/2017
This feels embarrassingly simple, but everything that I have tried doesn't work. Any help is appreciated.
I had the same problem as you and spent a whole week trying to solve it, because I couldn't found any guide on the internet. So I thought I could lend you a hand even though this was asked 9 months ago.
First things first: you need to do this with measures, as they're the only thing that is dynamically calculated within a PowerBI report. If you use a calculated column or a M formula inside the Query Editor, it will only be calculated whenever you refresh your data. They won't be affected by slicers and filters inside your report.
After trying a lot of stuff, I came up with the idea of making a measure that checks if each row is the most recent row for each group, then the measure will take a value of '1'. If it's not the last row, it will take a value '0'.
This is how I managed to do that (note: it was NOT that simple):
Build a calendar table that ranges from the minimum to the maximum of your date column. This must be done because if you use the date column itself in the next steps, the data will behave strangely. This new table's column will be used as the date slicer within the report:
Calendar = CALENDAR(MIN('Table'[TxDate]); MAX('Table'[TxDate]))
Now create a new measure in your data table that will filter the dates higher than the selected date in the report slicer and then it will find the latest date for each category or group:
Check =
VAR DateFilter = MAX('Calendar'[Date])
VAR LastDate = CALCULATE(
MAX('Table'[TxDate]);
ALLEXCEPT(
'Table';
'Table'[Name]
);
'Table'[TxDate] <= DateFilter
)
RETURN IF(LASTNONBLANK('Table'[TxDate]; 1) = LastDate; 1; 0)
If you build a table visual with all your 'Table' columns and LastDate as a measure, you will see that each row will have the most recent date of each group as a value for LastDate. Something like this:
Name Tx TxDate LastDate
---------------------------------------
A 1 1/1/2017 1/4/2017
A 2 1/3/2017 1/4/2017
A 3 1/4/2017 1/4/2017
B 4 1/5/2017 1/6/2017
B 5 1/6/2017 1/6/2017
C 6 1/1/2017 1/9/2017
C 7 1/2/2017 1/9/2017
C 8 1/9/2017 1/9/2017
It will be affected by the date slicer if you have one in your report. So the behaviour is right up to this point. Then I use the formula LASTNONBLANK() to give the date column a row context, thus being able now to compare between TxDate and LastDate and if both are the same, Check will take the value '1'.
Name Tx TxDate LastDate Check
---------------------------------------------
A 1 1/1/2017 1/4/2017 0
A 2 1/3/2017 1/4/2017 0
A 3 1/4/2017 1/4/2017 1
B 4 1/5/2017 1/6/2017 0
B 5 1/6/2017 1/6/2017 1
C 6 1/1/2017 1/9/2017 0
C 7 1/2/2017 1/9/2017 0
C 8 1/9/2017 1/9/2017 1
[Check] measure can't be used in a graph visual as of now, so we need to create another measure that will just count all rows in which [Check] takes the vale '1'. This is the simplest step as we just need a SUMX to sum all the rows of a filtered table:
Count of Tx = SUMX(
FILTER('Table'; [Check] = 1);
[Check]
)
Now you have the total count of all of your table rows with the most recent date within the range given by the date slicer. You can use this measure in a graph visual to show how many of those rows correspond to each category. For example, you are able to build a pie chart that shows how many "names" have the value "3" for Tx, how many have the value "5" and so on.
If you want a table visual that shows which is the latest date for each category, you can, instead of [Check], use [LastDate] as a measure like this:
LastDate =
VAR DateFilter = MAX('Calendar'[Date])
RETURN CALCULATE(
MAX('Table'[TxDate]);
ALLEXCEPT(
'Table';
'Table'[Name]
);
'Table'[TxDate] <= DateFilter
)
If you also want a measure that tells you which is the latest Tx for each category, you can change MAX('Table'[TxDate]) for MAX('Table'[Tx]). There are a lot of possibilities. My suggestion is that you study these formulas so you understand what they are doing in every step, so you can modify them for your needs.
You can also combine the two measures and get a table visual like this one:
Name LastTx LastDate
--------------------------
A 3 1/4/2017
B 5 1/6/2017
C 8 1/9/2017
Hope this helps you even 9 months after you asked, or maybe help other people with the same problem as we had.
I don't think you need to get that fancy. The following works for me.
LastTxDate = MAX(Table[TxDate])
Edit:
You'll want to take the max over the Tx values as well instead of listing all of them.
MaxTx = MAX(Table[Tx])
Use both MAX measures in your table instead of treating the Tx as another category.

PowerBi Change Card values to previous month Value if Current month value is not available

I am working on powerbi, using data over 2 months period, writing dax queries and trying to build reports.
I am trying to show the monthly values in cards visuals.
And i am able to do it using the measure below.
Sample Data:-
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
CustomKPI = IF(ISBLANK([PlanSum]),"No Data Available ",[PlanSum])&" "&IF([PlanSum]=[PlanPrevMon],"",IF([PlanSum] > [PlanPrevMon],UNICHAR(8679),UNICHAR(8681))&IF([PlanSum]<=0,"",""))
But here i don't want user to choose the month values from slicer.
I would like to show the card values as
if current month value is not available then it should compare with the previous month value automatically as shown in an image below
For example, Apr-2017 value is not available; in this scenario I would like it to compare with the Mar-2017 value.
If Mar-2017 value also not available, then the previous month value and so on
Edit
I tried what #user5226582 suggested but still getting wrong values as below image.
As you can see i restricted to crosscheck whether the value is getting in a right way of not.
But not.
This is the measure i have used as #user5226582 suggested.
c =
var temp = 'Revenue Report'[Date]
return CALCULATE(LASTNONBLANK('Revenue Report'[Planned Rev],
'Revenue Report'[Planned Rev]),
ALL('Revenue Report'),
'Revenue Report'[Date]<=temp)`
Can you please correct me if i am doing anything wrong
Does this help...?
To get the PlanPrevMon column to show like this, I first created a new index column:
id = COUNTROWS(FILTER(Sheet1, EARLIER(Sheet1[Date],1)>Sheet1[Date]))
Then I used the index to help create the PlanPrevMon column in two steps:
Step 1: I made one column named PlanPrevMon1.
PlanPrevMon1 = SUMX(FILTER(Sheet1,Sheet1[id]=EARLIER(Sheet1[id])-1),Sheet1[PlanSum])
Step 2: I made another column named PlanPrevMon.
PlanPrevMon = if(ISBLANK(Sheet1[PlanPrevMon1]),
if(Sheet1[id]=1,0,CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon1],Sheet1[PlanPrevMon1]),ALL(Sheet1),ISBLANK(Sheet1[PlanSum]))),
Sheet1[PlanPrevMon1])
For the card, I used this measure:
Card = CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon],1),FILTER(Sheet1,Sheet1[id]=max(Sheet1[id])))
I hope this helps.
You can use LASTNONBLANK DAX function.
Example data:
a b
1 1
2
3 2
4
5 3
Calculated column:
c =
var temp = Table1[a]
return CALCULATE(LASTNONBLANK(Table1[b],Table1[b]),ALL(Table1),Table1[a]<=temp)
Results in:
a b c
1 1 1
2 1
3 2 2
4 2
5 3 3