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

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

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.

DAX Power BI Conditional filtering

Sample file
The column c of a table T can have the values 1, 2 or 3. I want to filter the table such as when the user selects value 1 nothing is filtered. When the selected value is 2 then show only the rows with the c column containing the value 2 or the value 3 not the value 1 and finally if the selected value is 3 then show only those rows containing 3 in the c column. The slider or a plain filter must be single selection not multi because otherwise it would violate one of the user's business rule.
Selected Show
1 all rows
2 rows with 2 or 3
3 only rows with 3
I tried to create columns and to create measures but I can't get anywhere. Any directions?
I agree that the disconnected table might be the best workaround here. I hope this solution will work with your actual file.
Step 1 - create a new disconnected table
FilterC = DISTINCT(T[c])
Step 2 - make sure that your slicer is sourced from the new table (FilterC)
Step 3 - create a measure that will return 1 for rows that we want to see, and 0 for rows that we want to hide:
mFilter =
var Filter_C = SELECTEDVALUE(FilterC[c])
var Row_C = SELECTEDVALUE(T[c])
return
SWITCH(
Filter_C,
"1", 1,
"2", IF(OR(Row_C = "2", Row_C = "3"), 1, 0),
"3", IF(Row_C = "3", 1, 0)
)
Step 4 - add this measure to the table, or even table filters will suffice.
Step 5 - start switching the values!
First you need to set it to a number column.
Second, you could write complex measures and use a disconnected table, but the best option is to use the correct slicer. There is a Greater than or Equal To option.

DAX selecting and displaying the max value of all selected records

Problem
I'm trying to calculate and display the maximum value of all selected rows alongside their actual values in a table in Power BI. When I try to do this with the measure MaxSelectedSales = MAXX(ALLSELECTED(FactSales), FactSales[Value]), the maximum value ends up being repeated, like this:
If I add additional dimensions to the output, even more rows appear.
What I want to see is just the selected rows in the fact table, without the blank values. (i.e., only four rows would be displayed for SaleId 1 through 4).
Does anyone know how I can achieve my goal with the data model shown below?
Details
I've configured the following model.
The DimMarket and DimSubMarket tables have two rows each, you can see their names above. The FactSales table looks like this:
SaleId
MarketId
SubMarketId
Value
IsCurrent
1
1
1
100
true
2
2
1
50
true
3
1
2
60
true
4
2
2
140
true
5
1
1
30
false
6
2
2
20
false
7
1
1
90
false
8
2
2
200
false
In the table output, I've filtered FactSales to only include rows where IsCurrent = true by setting a visual level filter.
Your max value (the measure) is a scalar value (a single value only). If you put a scalar value in a table with the other records, the value just get repeated. In general mixing scalar values and records (tables) does not really bring any benefit.
Measures like yours can be better displayed in a KPI or Multi KPI visual (normally with the year, that you get the max value per year).
If you just want to display the max value of selected rows (for example a filter in your table), use this measure:
Max Value = MAX(FactSales[Value])
This way all filter which are applied are considered in the measures calculation.
Here is a sample:
I've found a solution to my problem, but I'm slightly concerned with query performance. Although, on my current dataset, things seem to perform fairly well.
MaxSelectedSales =
MAXX(
FILTER(
SELECTCOLUMNS(
ALLSELECTED(FactSales),
"id", FactSales[SaleId],
"max", MAXX(ALLSELECTED(FactSales), FactSales[Value])
),
[id] = MAX(FactSales[SaleId])
),
[max]
)
If I understand this correctly, for every row in the output, this measure will calculate the maximum value across all selected FactSales rows, set it to a column named max and then filter the table so that only the current FactSales[SaleId] is selected. The performance hit comes from the fact that MAX needs to be executed for every row in the output and a full table scan would be done when that occurs.
Posted on behalf of the question asker

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])

Creating a calculated table by passing a measure or parameter

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: