DAX Row Total is Wrong For Sum Date Difference - powerbi

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

Related

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

Cumulative of Cumulative measure power bi

Hi Team,
I need 2 output.
Output1 = Cumulative of No of Item.
Output2 = Cumulative of Output1.
Please help me.
If you have Date column from where you are picking the Month name, you can try this below 2 custom column code. I just considered first date of each month in the date column, but it will work for any date. this below column will give you cumulative vale per Year to Month-
Note- Based on your Date value, you may need some adjustment in the script. But using this logic, you should achieve your required output.
C1 =
var current_row_date_month = your_table_name[Date].[MonthNo]
return
CALCULATE(
SUM(your_table_name[No Of Item]),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[Date].[Year]),
your_table_name[Date].[MonthNo] <= current_row_date_month
)
)
C2 =
var current_row_date_month = your_table_name[Date].[MonthNo]
return
CALCULATE(
SUM(your_table_name[C1]),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[Date].[Year]),
your_table_name[Date].[MonthNo] <= current_row_date_month
)
)
Here is the sample output-

Sum row with next row value and grab next value in other column for the date selected

Let's say I have this data:
Earn Earn Cum.
13-Apr - -
14-Apr 48 48
15-Apr 257 305
16-Apr 518 823
17-Apr 489 1,312
18-Apr 837 2,149
19-Apr 1,005 3,154
20-Apr 1,021 4,175
21-Apr 1,463 5,638
22-Apr 2,630 8,268
23-Apr 2,993 11,261
24-Apr 3,354 14,615
25-Apr 4,332 18,947
26-Apr 4,885 23,832
27-Apr 4,514 28,346
28-Apr 4,356 32,702
29-Apr 4,824 37,526
30-Apr 7,082 44,608
1-May 6,091 50,699
2-May 1,407 52,106
When a date is selected in a dropdown slicer for example: 1-May I'd like to sum the rows 1-May with the next row 2-May for the column Earn and grab the next value 52,106 for the column Earn Cum.. The result should be:
1-May 7,498 52,106
Another example: if the date selected was 30-Apr the result must be:
30-Apr 13,173 50,699
I'm scratching my head trying to do this using a measure in Power BI.
I'll call your table "Data".
Create a measure:
Next Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE (
SUM ( Data[Earn] ),
Data[Date] = Current_Date || Data[Date] = Next_Date
)
Create another measure:
Next Cum Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE ( SUM ( Data[Earn Cum] ), Data[Date] = Next_Date )
Result:
Note: the code assumes that your dates are sequential (no gaps). If they have gaps, things are a bit more complex.
It will help to have access to date intelligence, so create a date table if you don't have one already. The following dax sets up a small table that just barely covers the sample data in your example.
Date = CALENDAR(Date(2019,4,10),Date(2019,5,5))
Create a relationship between the Dates in your table and the new Date dimension. Add a slicer to your visuals using the Date dimension.
We can use IF and ISFILTERED to check if filtering is being done. If we aren't filtering on Date then we get the normal table behavior. If we are, we'll see our modified result.
Earn _ Alt =
var tomorrow = CALCULATE (
SUM(Table1[Earn]),
dateadd('Table1'[Date], 1, DAY)
)
return Sum(Table1[Earn]) + IF(ISFILTERED`('Date'[Date]),tomorrow,0)`
and
Earn Cum. _ Alt = IF(ISFILTERED('Date'[Date]),
CALCULATE (
SUM(Table1[Earn Cum.]),
dateadd('Table1'[Date], 1, DAY)
),
SUM(Table1[Earn Cum.]))
Results:
-Unfiltered-
-Filtered-

DAX to calculate cumulative sum column (year to date) for all individual products

I have 3 columns in a table: Date, Product, and Volume. Based on this is I want to calculate the cumulative sum (or YTD) column for each of the products.
I know that to calculate cumulative total we use:
YTD_Actual = CALCULATE(Sum(Combined[Actual Volume]),FILTER(Combined,Combined[Date] <= EARLIER(Combined[Date])))
But I want this to additionally filter individual products and do this calculation for that product.
The expected column is shown in the picture below:
As far as you manage to get the Month column in date format, the following works:
YTD =
VAR currProduct = Table1[Product]
VAR currMonth = Table1[Month]
VAR ytdTotal =
CALCULATE(
SUM(Table1[Volume]),
FILTER(
ALL(Table1),
Table1[Month] <= currMonth &&
Table1[Product] = currProduct
)
)
RETURN IF(NOT(ISBLANK(ytdTotal)), ytdTotal,0)
Result:

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)