Calculation between minutes with a Power BI calculation - powerbi

I'm trying to calculate the time difference between two rows from the row below to the one above bearing in mind the end of quarter or the change in Game, but I'm not being available to do it.
The Google Drive Link with the xlsx: https://drive.google.com/drive/folders/1SlzwO6bgBBgrftwrVPGRVBE9KkIy_YqJ?usp=share_link
Example of the expected Result with a Game (ExpectedResult Tab in xlsx). The ExpectedResult is calculated with the difference between the cell and the one above, bearing in mind the end of quarters or the change in games.
Once we could calculate the ExpectedResult value, I would like to sum up the value (like it was a Pivot Table in Excel).

You can try this measure-
M =
var current_quarter = min(your_table[Full_Quarter])
var current_time = min(your_table[Time_Def])
var prev_time =
CALCULATE(
MAX(your_table[Time_Def]),
FILTER(
ALL(your_table),
your_table[Full_Quarter] = current_quarter
&& your_table[Time_Def] < current_time
)
)
return current_time - prev_time
And set the measure type as marked in the below image-

Related

Calculating single values from cumulative data in Power BI

In my sales table I would like to change cumulative values into single values. Here is sample data from my table.
I created a measure that as far as I know should works for this.
sales_single_values = VAR current_sales = SUM('sales'[sales cumulative]) VAR prev_sales SUM('sales'[sales cumulative]) - CALCULATE( current_sales, 'sales'[period] = 'sales'[period] - 1) Return IF(ISBLANK(prev_sales), BLANK(), current_sales - prev_sales)
But unfortunately the final result on the chart is still the same as I used cumulative values, not single ones. Any ideas what should I change in my measure?
Expected values would be:
Period 1: 4
Period 2: 2
Period 3: 7
As a measure, something like:
sales_single_values =
var prev = max('sales'[period]) - 1
var prev_sales = CALCULATE( SUM('sales'[sales cumulative]), 'sales'[period] = prev)
Return
sum(sales[sales cumulative]) - prev_sales
But this seems like more of a modeling transformation, so do it in PQ, or with a calculated column, like
current sales =
var prev = calculate( max('sales'[period]) ) - 1
var prevSales = calculate( sum(sales[sales cumulative]), all(sales), sales[period] = prev)
return
sales[sales cumulative]-prevSales

Why does recalculating a variable using a Calculate within a single measure return the wrong answer?

I'm trying to calculate a 12 month trailing sum. When I use CALCULATE to sum account balances over this 12 month period I get different results for when I put in just the variable name as opposed to just adding the code that makes that variable into the Calculate function. I though they should be the same because they are both referring to the same code. Can someone please explain this?
This block returns the undesired value (This is where I'm using the variable name of Sales in the Calculate):
TTM Revenue =
Var Sales =
CALCULATE(
SUM(Fact_FinancialStatement[Period Balance]),
Dim_FinancialAccounts[UD1] = "Sal"
)
Var SelectedPeriod =
SELECTEDVALUE(Dim_FiscalPeriod[FiscalPeriodIndex])
Var TTM =
CALCULATE(
Sales,
ALL(Dim_FiscalPeriod),
Dim_FiscalPeriod[FiscalPeriodIndex] >= SelectedPeriod - 11 &&
Dim_FiscalPeriod[FiscalPeriodIndex] <= SelectedPeriod
)
RETURN
TTM
This next block return the desired value but I don't understand why it's different than the first.
TTM Revenue =
Var SelectedPeriod =
SELECTEDVALUE(Dim_FiscalPeriod[FiscalPeriodIndex])
Var TTM =
CALCULATE(
SUM(Fact_FinancialStatement[Period Balance]),
Dim_FinancialAccount[UD1] = "Sal",
ALL(Dim_FiscalPeriod),
Dim_FiscalPeriod[FiscalPeriodIndex] >= SelectedPeriod - 11 &&
Dim_FiscalPeriod[FiscalPeriodIndex] <= SelectedPeriod
)
RETURN
TTM
Without any data sample provided my answer will be fully theoretical.
In DAX variables are not just 'shortcuts'. You can use them to build your desired calculation logic with proper context.
In your first example Sales are calculated separately from TTM calculation context. The only filter being used there is Dim_FinancialAccounts[UD1]. Then calculation result is placed inside TTM calculation and gets another calculation context for values calculated previously.
In your second example you mix two contexts which have affect combined.

Last month average constant in Power-Bi

I am working in Power-Bi desktop. I need to compute a constant measure (same for all rows) that indicates me the daily average of the amounts stored in a column named "Values" considering the last month as window. The distinct-count for the days of the last month has to exclude the last two days of the week. The window has to start Today() - [DELTA], where DELTA is a constant number of days.
An example below (which is rolling, not constant, and does not include DELTA):
dailyAverageValue =
var nobs = CALCULATE(DISTINCTCOUNT(TABLE[DATE]), PREVIOUSMONTH(TABLE[DATE]), TABLE[WEEK_DAY_NO] <> 6, TABLE[WEEK_DAY_NO] <> 7)
var summ = CALCULATE(SUM(TABLE[VALUES], PREVIOUSMONTH(TABLE[DATE]))
return summ/nobs
dailyAverageValue =
var nobs = CALCULATE(DISTINCTCOUNT(TABLE[DATE]), DATESINPERIOD(TABLE[DATE], TODAY() - [DELTA], -[WINDOW], DAY))
var summ = CALCULATE(SUM(TABLE[VALUES]), DATESINPERIOD(TABLE[DATE], TODAY() - [DELTA], -[WINDOW], DAY))
return summ/nobs

DAX Row Total is Wrong For Sum Date Difference

I have table which includes start_date and end _date. I want to get date difference between these columns by minute. There is also date and time slicer and if max date slicer is smaller than end_date, I have to get date difference between start date and max date slicer + max time slicer.
I created a measure about this but row total is not true. How can I fix it?
this is my measure I wrote;
Zaman 3 =
var MaxDateTime =MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time])))
var sonuc1 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE))
var sonuc2 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDateTime,MINUTE))
var deger = IF(MaxDateTime>MAX(vwFactATM_STATUS_LOG[END_DATE]),sonuc1,sonuc2)
//var a=MAX(vwFactATM_STATUS_LOG[END_DATE])
return deger
And table output;
As You see row total is not true. How can I fix it?
I solved the problem. We need calculate inside "if block" look I how I corrected
Var MaxDate=CALCULATE(MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time]))),ALLSELECTED(vwDimDate),ALLSELECTED(vwDimTime))
VAR Tarih=Sumx(
vwFactATM_STATUS_LOG, If(MaxDate<vwFactATM_STATUS_LOG[END_DATE], DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDate, MINUTE) , DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE) ))
return Tarih

Calculate sales of products that sold this year but not the year before

I want to calculate the sum of NetSales for the products that had sales this year (2019) but NOT sales last year (2018).
This is what I'm trying (and a million variations more similar to this):
NetSales CY Not LY =
VAR ThisYear= 2019
VAR YearBefore= 2018
VAR TabelaThisYear = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=ThisYear && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR TabelaYearBefore = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=YearBefore && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR ProdutosOnlyThisYear = EXCEPT(TabelaThisYear;TabelaYearBefore)
RETURN
CALCULATE(SUM(Facts[NetSalesCurr]);ProdutosOnlyThisYear)
I would use the following approach, where we don't hardcode the years:
NetSales CY Not LY=
CALCULATE(
SUM(Facts[NetSalesCurr]),
FILTER(
VALUES(Facts[ArticleNo]),
CALCULATE(
COUNTROWS(Facts),
PREVIOUSYEAR(Calendar[Date])
) = 0
)
)
Let's break it down:
The inner FILTER function iterates through VALUES(Facts[ArticleNo]) - that is all ArticleNo's that have sales in the currently selected period.
For every one of those ArticleNo's, we calculate the number of rows on the fact table for the previous year. CALCULATE(COUNTROWS(Facts), PREVIOUSYEAR(Facts[InvoiceDate]))
We keep only those ArticleNo's where this number is = 0.
The list of ArticleNo's returned from FILTER is then used in the outer CALCULATE statement, so that we only get the sum of NetSales for articles that did not have any sales in the previous year.
For more information, please see the New and returning customers-pattern.
you can use the dax formula like at the below , if you have a calendar table ;
this year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),0,YEAR)
last year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),-1,YEAR)