Latest purchase date per category & month - Power BI - powerbi

Let's assume I have the below dataset:
How can I get the latest purchase date per item and month only for those items that >0?
Expected result:
I've tried a few solutions, one of them, but I cannot figure out all three conditions to be met:
latest_purchase_date = CALCULATE(
MAX(tbl[date]),
FILTER(ALL(tbl),
tbl[quantity]<>0))
Error:
Error:

Did you try the following:
VAR currMonth = MONTH(SELECTEDVALUE(tbl[date]))
VAR currYear = YEAR(SELECTEDVALUE(tbl[date]))
VAR currItem = SELECTEDVALUE(tbl[item])
RETURN
CALCULATE (
MAX(tbl[date]),
FILTER(
ALL(tbl),
tbl[quantity]<>0 &&
tbl[item] = currItem &&
MONTH(tbl[date]) = currMonth &&
YEAR(tbl[date]) = currYear
)
)

Related

Trying to get list of Venues with respect to Suppliers

Hi guys so i am trying to build a report where i have to show new customers this month compared to last month. I am able to calculate new customers fine but i have to show them in a Matrix in Power Bi. This is the Code i am using
New Customers This Month =
VAR ThisMonth =
SELECTEDVALUE( DailyReport[DateCreated].[MonthNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastMonth = ThisMonth - 1
VAR CustomersThisMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = ThisMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = LastMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersThisMonth, CustomersLastMonth )
RETURN
CONCATENATEX( T1, [C1], ", " )
I am getting result like this, two columns one for supplier and one for new venues this month
getting result like this
But expected result should be like this, you know like a list
expected result
i think it may be causing due to how i am concatening it in the last line in code
and please let me know if you need any more information

grand total empty in table

I've the following pbix https://1drv.ms/u/s!Amd7BXzYs7AVlXPjuP0CfblYyc5K?e=kNnEiN
I've the following measure 
Measure2 = var _1=max(MaxPropre[Nb]) +0
var _min = minx(ALLSELECTED('Date Filter'), 'date Filter'[Date]) var ax = maxx(ALLSELECTED('MaxPropre'), 'MaxPropre'[Date])
var _max = maxx(ALLSELECTED('date Filter'), 'date Filter'[Date]) VAR dt =SELECTEDVALUE ( 'DimDate'[date] )
return
CALCULATE(if((max('DimDate'[Date]) <_min || max('DimDate'[Date]) >_max ) , BLANK(), if(ax >=dt,[const],0)))
 
why I'm not getting the grand total please?
You can add another measure like:
Measure3 = IF(HASONEFILTER('DimDate'[date]),[Measure2],SUMX(VALUES('DimDate'[date]),[Measure2]))
...and use it in your table.
This site explains what you're dealing with fairly well.

How do I get the period difference for each item in the table in PowerBI desktop?

As seen below, I need to find the difference in quantities for each individual fruits with respect to the previous value. I have previously tried using in built Power BI functions like "PreviousDay()" but haven't found success yet.
You can create these below 2 Measure for your purpose-
Pervious_Quantity =
Var current_row_date = MIN(your_table_name[Date])
var previous_date =
CALCULATE(
MAX(your_table_name[Date]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] < current_row_date
)
)
RETURN
CALCULATE(
SUM(your_table_name[Quantity]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] = previous_date
)
)
Difference = min(your_table_name[Quantity]) - if([Pervious_Quantity] = BLANK(),0, [Pervious_Quantity])
Here is the final output-

DAX - Plot Previous working day value against today's date

I have a table like this,
ReportingDate ReportingDateOrder Status Customer
01/06/2021 1 Active Present
01/06/2021 1 Active
01/06/2021 1 Inactive Present
27/05/2021 2 Inactive Present
27/05/2021 2 Active Present
27/05/2021 2 Active Present
26/05/2021 3 Active Present
I want to generate an visual table like this,
ReportingDate PreviousDaySales
01/06/2021 2
27/05/2021 1
26/05/2021 0
The logic for previousdaysales is the count of active lines for the previous available reporting date.
This is what I have tried so far, but it is returning empty.
PreviousDaySales =
var selectedreportingdate = SELECTEDVALUE('Table1'[Reporting Date])
var selectedreportingrank = CALCULATE(MIN('Table1'[ReportingDateOrder]),FILTER('Table1', 'Table1'[Reporting Date] = selectedreportingdate))
var old_rank = selectedreportingrank + 1
var val1 = CALCULATE(COUNT('Table1'[Action Status]), FILTER('Table1', 'Table1'[Status] = "Active" && 'Table1'[ReportingDateOrder] = old_rank))
return val1
Kindly help me with this.
CurrentActiveLines :=
CALCULATE(COUNTROWS(RStatus),RStatus[Status]="Active")
PreviousReportingDateActiveLines :=
VAR CurrentDate = SELECTEDVALUE( RStatus[ReportingDate] )
VAR PreviousDate =
CALCULATE(
LASTDATE( RStatus[ReportingDate] ),
RStatus[ReportingDate] < CurrentDate
)
VAR Result =
CALCULATE(
COUNTROWS( RStatus ),
RStatus[ReportingDate] = PreviousDate,
RStatus[Status] = "Active"
)
RETURN
Result

How to calculate Dynamic Moving Average without any date reference?

I am looking for some clarification with regards to moving average calculation. My data looks like the screenshot attached.
I checked online for suggestion but it went awry. Most of the moving averages work with date (in date format). As I am having Day...only in numeric format...I am not sure which function would help me to get Mov_Avg for 3 Day time frame on the Increment col. My desired output should be col 3_day_Avg
You can use your column Increase (created using Power Query) and create this below measure for your required output-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Here is the final output-
Alternatively, if you wants not to use Power Query, You can use Custom column and that case you have to convert your Measure increase_using_measure as a column. As aggregation is required, you can not refer a measure for that. Here below is the code for column-
increase_using_column =
VAR current_row_day = your_source_table_name[day]
VAR current_row_country = your_source_table_name[country]
VAR current_row_case_death = your_source_table_name[cases/death]
VAR current_row_count = your_source_table_name[count]
VAR previous_day_count =
LOOKUPVALUE(
your_source_table_name[count],
your_source_table_name[day],current_row_day-1,
your_source_table_name[country],current_row_country,
your_source_table_name[cases/death],current_row_case_death
)
RETURN if(previous_day_count = BLANK(),current_row_count, current_row_count - previous_day_count)
Now you can use the above column created using DAX in your Last 3 days average calculation as below-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase_using_column]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Output will be same as shown above.