I am new to Power BI and the language DAX. Therefore, I find it hard to do simple yearly average across different products.
I have tried a different combination of the functions AVERAGEX and CALCULATE without any luck so far.
My table:
Date | Product | Value
2014-05-06 Cheese 10
2014-08-11 Cheese 12
2015-04-11 Cheese 9
2014-01-22 Milk 4
2014-12-24 Milk 8
The output I try to create:
Date | Product | Value | Yearly_AVG
2014-05-06 Cheese 10 11
2014-08-11 Cheese 12 11
2015-04-11 Cheese 9 9
2014-01-22 Milk 4 6
2014-12-24 Milk 8 6
Lastly, I do not have a calendar table in the dataset.
For a measure, you can write something like this:
YearAvg =
VAR CurrProduct = VALUES ( Sales[Product] )
VAR CurrYear = YEAR ( MAX ( Sales[Date] ) )
RETURN
CALCULATE (
AVERAGE ( Sales[Value] ),
ALLSELECTED ( Sales ),
Sales[Product] IN CurrProduct,
YEAR ( Sales[Date] ) = CurrYear
)
Related
This data
Row_ID EmployeeID Date
1 1 2023-02-13
2 2 2023-02-13
3 3 2023-02-13
4 1 2023-01-13
5 8 2023-01-13
6 7 2023-01-13
7 4 2023-01-13
8 5 2023-01-13
9 6 2023-01-13
and a DATE table
I use a Measure
_DistinctEmployee = DISTINCTCOUNT(tblTestEmployee[EmployeeID])
And a Messure for the DATESTYD
_DATESTYD = CALCULATE([_DistinctEmployee],DATESYTD(vwDimDatum[Datum2]))
Image for date choise and table
The end result i want on the measure (NOT drag in MONTH in the table)
Is that the datestyd take the distinct numbers of employee (Becouse in the real scenarie there can be more then one employee that month)
and then + that month with the next month and then + it to the next month thats accumulated
and then take al this addition of values and devide it with the number if the month thats choisen.
Example of the data
I have choisen MONTH Febuary
In my measure i get the number 8 and thats the distinct accumulated employees in JAN and FEB and if i devide that with the number of the month i get 4 and that wrong that no the avrage.
there is 3 distinct employee in January
and there is 6 Distinct employee in Febuary
3+6 / 2(Febuary) = 4,5
Thats the average of employees in al this month.
Try the following measure:
=
VAR DateChosen =
MIN( vwDimDatum[Datum2] )
VAR T1 =
SUMMARIZE(
FILTER( ALL( vwDimDatum ), vwDimDatum[Datum2] <= DateChosen ),
vwDimDatum[Datum2],
"Distinct Employees", [_DistinctEmployee]
)
RETURN
AVERAGEX( T1, [Distinct Employees] )
Given a table FACT in Power BI with three columns Date, Category and Sales I am looking for a DAX function that for each day returns the sum of the sales of its previous n days.
Lets assume n = 2, that means for day 01/04/2020 my measure should return the sum of the sales of the days 01/02/2020 and 01/03/2020.
Here is a small example:
Date Category Sales
01/01/2020 A 1
01/01/2020 B 3
01/02/2020 B 2
01/03/2020 B 1
01/04/2020 A 0
01/05/2020 B 10
01/06/2020 B 7
What I want is
Date MyMeasure
01/01/2020 0
01/02/2020 4
01/03/2020 6
01/04/2020 3
01/05/2020 1
01/06/2020 10
I later would then like to use the Category as a filter and in my case n is 365.
I tried the following
MyMeasure =
VAR FROM_DATE =
DATEADD ( FACT[DATE], 0, DAY )
VAR SALES_365 =
CALCULATE (
SUM ( FACT[SALES] ),
DATESINPERIOD ( FACT[DATE], FROM_DATE, 365, DAY )
)
RETURN
SALES_365
but got an error that a table of multiple values was supplied where a single value was expected.
The error was in the variable FROM_DATE, in which DATEADDD returned a column of dates and not a single value.
That said you don't really need a variable for that, you can just use a simple subtraction and DATESBETWEEN(DateCol, DateFrom,DateTo)
MyMeasure =
CALCULATE (
SUM ( FACT[SALES] ),
DATESBETWEEN('FACT'[DATE],MAX(FACT[DATE])-2,MAX(FACT[DATE])-1)
)
New to DAX. I need a bit of help when moving excel formulas to DAX. I have this table and call it table1:
Date ID Time
10/06/2020 1 1
10/06/2020 1 2
11/06/2020 1 3
12/06/2020 2 5
13/06/2020 1 6
And I need to calculate count and average per day per ID. The results with two simple formulas (=COUNTIFS(B:B,B2,A:A,A2)) and (=AVERAGEIFS(C:C,B:B,B2,A:A,A2)) in excel is:
Date ID Time Count ifs Avergae ifs
10/06/2020 1 1 2 1.5
10/06/2020 1 2 2 1.5
11/06/2020 1 3 1 3
12/06/2020 2 5 1 5
13/06/2020 1 6 1 6
How can I replicate in DAX?
Update
Count in DAX
DAX Count =
COUNTROWS (
FILTER (
'Table1',
'Table1'[ID] = EARLIER ( 'Table1'[ID] )
&& 'Table1'[Date] = EARLIER ( 'Table1'[Date] )
)
)
Sum per day per ID
Sum =
CALCULATE(
SUM(Table1[Time]),
FILTER(
All(Table1),
Table1[ID] = EARLIER(Table1[ID]) &&
Table1[Date] = EARLIER(Table1[Date])
)
)
Item closed :)
I have answered the question. #Alexis Olson thank you for the inspiration.
Count in DAX
DAX Count =
COUNTROWS (
FILTER (
'Table1',
'Table1'[ID] = EARLIER ( 'Table1'[ID] )
&& 'Table1'[Date] = EARLIER ( 'Table1'[Date] )
)
)
Sum per day per ID
Sum =
CALCULATE(
SUM(Table1[Time]),
FILTER(
All(Table1),
Table1[ID] = EARLIER(Table1[ID]) &&
Table1[Date] = EARLIER(Table1[Date])
)
)
[http://uupload.ir/view/v2t_capture.png] We have 2 tables with 2 columns:
table location ->column ->city ,province
table sales column ->actual sales
table location : table sales
province city id sales
tehran eslamshar 1 100
tehran rey 2 500
hamedan tefresh 3 500
esfahan esahan 4 400
gilan rasht 5 400
gilan rar 6 900
I want to calculate 80% of total sales in each city and each province
it means if we have 2800 total sale in whole province, we want just show province that cut off 80 % of sales:
I use this measure but it has one problem that duplicate value does not count in a cumulative sum.
If I have two same sale like 900 it does not count in cumulative and it ignores it.
Cumulative Total =
IF (
NOT ( ISBLANK ( [sales] ) ),
CALCULATE (
[sales],
FILTER (
ALL ( DimLocation[Province] ),
CALCULATE ( [sales], VALUES ( DimLocation[Province] ) ) <= [sales]
)
)
)
Expected Result:
sales cumulative
tehran 800 800
shiraz 200 1000
ghom 200 400
markazi 300 500
Output:
tehran 800 800
shiraz 200 1000
ghom 200 1000
markazi 300 500
For a running total you could create this measure:
RT Province Sales :=
VAR _current = SELECTEDVALUE ( DimLocation[Province] )
RETURN
CALCULATE (
[Sales],
DimLocation[Province] <= _current ,
ALL ( DimLocation[Province] )
)
I need to sum each month the ID's amounts that meet the next criterias:
Date End < Agreement date
Date End < Month to show
I have tried with filter, sum and others options, but the result is the same.
My datas are:
ID START DATE AGREEMENT DATE END DATE AMOUNT
1 09/15/2018 01/01/2019 02/20/2019 100
2 09/20/2018 01/15/2019 12/01/2019 100
3 10/01/2018 03/01/2019 12/01/2019 100
4 10/01/2018 03/20/2019 05/01/2019 100
5 11/10/2018 07/10/2019 100
6 09/15/2018 04/05/2019 05/01/2019 100
7 10/01/2018 06/10/2019 05/01/2019 100
8 10/20/2018 07/11/2019 04/10/2019 100
9 11/11/2018 08/01/2019 100
10 11/01/2018 06/01/2019 04/10/2019 100
This is my DAX code:
Result =
CALCULATE (
SUM ( 'Report Diario'[Amount] );
USERELATIONSHIP ( Calendario[Date]; 'Report Diario'[End Date] );
FILTER (
'Report Diario';
'Report Diario'[End Date] < 'Report Diario'[Agreement date]
)
)
finally, I have created a new table and the next calculated column works:
Result = CALCULATE(SUM('Report Diario'[Amount]);FILTER('Report Diario';AND(Calendario[Date]<'Report Diario'[Agreement Date];AND(Calendario[Date]>'Report Diario'[End Date];NOT(ISBLANK('Report Diario'[End Date]))))))