Sum of a measure per date range - powerbi

I have the following Table Visualization.
I'd like the table to look like the following. Column C should be averaging the range of Column B.
For example:
C2 = AVERAGE(B2:B2)
C3 = AVERAGE(B2:B3)
C4 = AVERAGE(B2:B4)
and so on.
The Year-Month column is from my MonthTable. The schema is as follows,
And the Sum measure DAX is as follows,
For the CumulativeSum measure, I have tried the following.
CumulativeSum =
CALCULATE(
[Sum],FILTER(AppendedTables,AppendedTables[Year-Month] <= MAX(AppendedTables[Year-Month]))
)
I'm guessing the issue is my CALCULATE([SUM]) area. I wanted to wrap [SUM] in a SUM() method, but that doesn't work. It gives the error "The SUM function only accepts a column reference as the argument number 1".
Please enlighten me.

I've been able to produce your desired results by creating a calculated column using the following code:
CumSum =
VAR CntRow =
COUNTROWS ( FILTER ( Sheet1, [Year-Month] >= EARLIER ( [Year-Month] ) ) )
VAR CumSum =
CALCULATE (
SUMX ( Sheet1, Sheet1[Sum] ),
FILTER ( Sheet1, Sheet1[Year-Month] >= EARLIER ( Sheet1[Year-Month] ) )
)
RETURN
DIVIDE ( CumSum, CntRow )
Hope this helps!!

Related

DAX Measure: case based on other column text values

I have a big model in PowerBI where there are many different aggregation and grouping based on columns being displayed or not on the final table.
Simplifying: I need to do a conditional statement doing the sum if the value of column 1 is A1 but doing the MAX() if the value of column 1 is A2.
I need to have that information in the same column of the final output.
How would you go for this one?
Thank you very much for your help!
if you have only two values you can do a simple IF like this :
Measure = IF ( SELECTEDVALUE('Table'[Column1]) = "A1", SUM('Table'[Column2]), MAX('Table'[Column2]))
Please try this code:
TblMeasure =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( FactTable[Column1] ),
"Test",
IF (
FactTable[Column1] = "A1",
SUM ( FactTable[Column2] ),
MAX ( FactTable[Column2] )
)
)
RETURN
SUMX ( TblSummary, [Test] )
If we test it on a table visual:

Return date based on a condition in Power BI using DAX

hope you can help me.
I need to calculate in Power BI a date difference between today() and a certain date based on a condition.
I have a calendar table with the date (calendario[fecha]) related to a fact table ASID to predict column ASID[amount] and a measeure [Estimado] that gives me the linear regression
Estimado =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( 'calendario'[fecha] ),
"Known[X]", calendario[fecha],
"Known[Y]", [ASID]
),
AND (
NOT ( ISBLANK ( Known[X] ) ),
NOT ( ISBLANK ( Known[Y] ) )
)
)
VAR Count_Items =
COUNTROWS ( Known )
VAR Sum_X =
SUMX ( Known, Known[X] )
VAR Sum_X2 =
SUMX ( Known, Known[X] ^ 2 )
VAR Sum_Y =
SUMX ( Known, Known[Y] )
VAR Sum_XY =
SUMX ( Known, Known[X] * Known[Y] )
VAR Average_X =
AVERAGEX ( Known, Known[X] )
VAR Average_Y =
AVERAGEX ( Known, Known[Y] )
VAR Slope =
DIVIDE (
Count_Items * Sum_XY - Sum_X * Sum_Y,
Count_Items * Sum_X2 - Sum_X ^ 2
)
VAR Intercept =
Average_Y - Slope * Average_X
RETURN
ROUND(
SUMX (
DISTINCT ( calendario[fecha] ),
Intercept + Slope * calendario[fecha]
),0)
My visualization matrix has 3 columns: calendario[fecha], it's real value [ASID] and the estimated measure [Estimado].
I have a limit of 1105 for that ASID.
I can see that at a future day, let's say a month from now 03/12/2020, the estimated reaches a value of 1105 (after scrolling all the matrix), so I need a way to capture that day and be able to calculate 03/12/2020 - today() and display somewhere: "30 days left"
Raihan: I could use the datediff as you suggested
matrix
Is there a way to capture just the 231 value?
DAX is now: if([Estimado]>1105, DATEDIFF(TODAY(),LASTDATE(calendario[fecha]),DAY),0)
As you didn't provide the sample dataset and you didn't tell about your measure formula I just consider a sample dataset on my own to simulate your problem.
Consider following screenshot with data and calculated columns.
Here the DaysFromToday calculate the Day difference from Today to for every column if the corresponding value in 'SomeValue' field reached a certain number. SomeValue is also calculated field that you can replace with your own calculation.
To get the single value from DaysFromToday you can have a measure which will give you MAX or MIN (of course some others functions if you need) of the column values like following screenshot -
Yellow highlighted is the DAX way of mentioning a field name with the table name that you are missing in your formula.
Lastly the MAX or MIN measure can be displayed in the report like following -
it will be better if you can provide sample dataset and sample answer that you want.

filtering measures based on two columns in power bi dax

I want to use a measure and filter the result based on the columns:
My measure is :
TotalProductionCon =
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] )
and I want it to summarize only when column année = column year.
I tried CALCULATE and FILTER;
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] );
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
but it generates an error that the columns contain much value and I need to use aggregation.
Can you help me?
The problem with your formula is that you limited ALL function to only one column (Annee), and as a result FILTER does not "see" the other column it needs.
To fix that, change your formula as follows:
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL (
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année];
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
);
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
I am assuming here that your choice of ALL function is appropriate; otherwise you might need to use a different technique such as SUMMARIZE function.

CountIf formula in DAX

I would like to count the frequency of a value in a column for each row. In Excel my situation can be solved with this formula:
=COUNTIF(I:I;I4)
In PowerBi Report and I have a table of students with a column, "Pääaine" (main subject). There are 81 distinct values in 1580 rows. I would like to calculate the number of similar students for each row (so that I can filter out the main subjects that have 4 or less students).
How do I do it in PowerBI?
With Calculated column like this I get 1580 for each cell:
Pääaine lkm =
CALCULATE(
COUNTROWS(Opiskelunkulku);
FILTER(
Opiskelunkulku;
Opiskelunkulku[Pääaine] = Opiskelunkulku[Pääaine]
)
)
You can use COUNTROWS() and EARLIER() to achieve this. EARLIER() returns the value for the specified column in the current row context.
Pääaine lkm =
COUNTROWS (
FILTER (
Opiskelunkulku,
Opiskelunkulku[Pääaine] = EARLIER ( Opiskelunkulku[Pääaine] )
)
)
As an alternative to Rory's answer try CALCULATE() with ALLEXCEPT() as a filter. Like this:
Pääaine lkm =
CALCULATE (
COUNTROWS ( Opiskelunkulku ),
ALLEXCEPT ( Opiskelunkulku, Opiskelunkulku[Pääaine] )
)

DAX if else for measure

How to use if else for DAX in the measure. If row value =1 then take the var a calculated value else take the var b calculated value
x:=var a=[DATA1]
var b=[DATA2]
return(if([HOUR]=1),a,b)
I get error using above formula
It seems your problem is that you are not aggregating the columns while creating the measure. Measures only works aggregating data in a given context, generally if you want to perform calculations per row you should use a calculated column instead of a measure.
And the DAX expression for a calculated column should be:
MyColumn = IF([HOUR] = 1, [DATA1], [DATA2])
Otherwise if you want to use a measure you have to explicitely aggregate the column values in the given context, i.e:
MyMeasure =
VAR a =
FIRSTNONBLANK ( ExampleTable[Data1], 0 )
VAR b =
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
RETURN
IF ( SUM ( ExampleTable[Hour] ) = 1, a, b )
Or simply:
MyMeasure =
IF (
SUM ( [Hour] ) = 1,
FIRSTNONBLANK ( ExampleTable[Data1], 0 ),
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
)
Let me know if this helps.