I am trying to create two columns for each product, one that reflects current year sales and another that shows prior year sales.
My underlying data already aggregates all sales by year. In this example I have three tables. One that contains the main sales. And the other is just a measure table where I store all my measures related to sales in Denver. So Denver Sales[Sales] is essentially just a measure which filters the 'Main Sales' table for Denver data only. This measure if defined as follows:
Sales = CALCULATE(SUM('Main Sales'[Sales]),
FILTER('Main Sales', 'Main Sales[City] = "Denver")
)
The third table is the Product table, which only contain two columns Product Name and Product ID. It has a relationship with Main Sales based on the Product ID.
So in order to get the PY sales, I tried to create a measure with the following DAX code:
PY Sales =
VAR py_sales =
CALCULATE(
'Denver Sales'[Sales],
FILTER ( ALLSELECTED('Main Sales'), 'Main Sales'[Year] = MAX ( 'Main Sales'[Year]) -1 )
)
RETURN
py_sales
However, as you can see below. The PY Sales column is taking the sum of all 3 products' PY sales for each year, rather than just the individual product.
Can anyone help me understand why my code is doing this?
I have following questions.
Table Sales:
Table Product:
Table Sales is connected to table Product via Sales ID.
Table Sales contain 5 unique records, table Product contain only 3 of them. 2 are missing. Is there some way how to artificially create in Power BI missing category?
I am trying to determine if a date is between 2 other dates that are is 2 different tables using
Power BI.
For simplicity, here is the model that I have :
TableB is the bridge table between TableA and TableC.
I have an inactive relationship between tableA and TableC.
I have tried the following logic to check if TableC.createdDate is between TableA.startDate and TableA.endDate :
Create a calculated column in TableC, but I was not able to access columns in TableA
Create a calculated column in TableB, but I was having blank results, which is not suppose to happen
Have you tried using USERELATIONSHIP to force your calculated column to use the inactive relationship to TableA? documentation link
So something like
CALCULATE(
{{created date is >start and <end}},
USERELATIONSHIP('TableA'[TableB_ID],'TableC'[TableB_ID]
)
I have a date table that is used to populate a date slicer. The date slicer filters all other filters on the page but one. The one it doesn't filter has a one-to-many relationship back to the date table.
However, when I use the data from table two as an axis (Date and Hour) it actually displays all date/hours from the entire table but doesn't restrict the date/hour range to that of the parent date table (table one). Thoughts on how I can achieve this without using merge tables (preferable in DAX)?
In SQL Server I would do the following to achieve this output:
select fc1.calendardatewithtime, totaltable.total
from FiscalCalendarWithTime fc1
cross apply (
select top(1) count(distinct id) total
from ActionDetail ad1
where ad1.upgraded_on=fc1.calendardatewithtime and ad1.status=3
)as totaltable
where exists ( select 1 from dbo.FiscalCalendarTable fc2 where fc2.calendardate=fc1.calendardate and fc2.fiscalweek=1 )
order by fc1.calendardatewithtime asc;
Where calendardatewithtime is the date with time field that I would use as the axis and totaltable.total is the value I would display as the graph total.
My Date Slicer code:
SpecialDateDropdown =
VAR _datetable = FiscalDGCalendar
VAR _today = TODAY()
VAR _yesterday = TODAY()-1
VAR CurrentFiscalWeek = calculate(min(FiscalCalendar[fiscal_week]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalPeriod = calculate(min(FiscalCalendar[fiscal_period]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalQuarter = calculate(min(FiscalCalendar[fiscal_quarter]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalYear = calculate(min(FiscalCalendar[fiscal_year]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
RETURN UNION(
ADDCOLUMNS(FILTER(_datetable,[fiscal_date]=_today),"Period","Today","Order",1),
ADDCOLUMNS(FILTER(_datetable,[fiscal_date]=_yesterday),"Period","Yesterday","Order",2),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_week]=CurrentFiscalWeek),"Period","Current Fiscal Week","Order",3),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_week]=CurrentFiscalWeek-1),"Period","Prior Fiscal Week","Order",4),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_period]=CurrentFiscalPeriod),"Period","Current Fiscal Month","Order",5),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_period]=CurrentFiscalPeriod-1),"Period","Prior Fiscal Month","Order",6),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_quarter]=CurrentFiscalQuarter),"Period","Current Fiscal Quarter","Order",7),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear),"Period","Current Fiscal Year","Order",8),
ADDCOLUMNS(_datetable,"Period","Custom Date Range","Order",9)
)
And this is how I am gathering my totals so the only piece I am missing is how to visually display only a specific date range:
CALCULATE(
IF(
ISBLANK(DISTINCTCOUNT(ActionHistoryDetail[id])),0,DISTINCTCOUNT(ActionHistoryDetail[id])
),ActionHistoryDetail[status]=3,USERELATIONSHIP(FiscalCalendar[fiscal_date],fiscalcalendarwithtime[CalendarDate]),USERELATIONSHIP(fiscalcalendarwithtime[CalendarDateWithTime],ActionHistoryDetail[upgraded_on])
)
You can group the slicers in a single family, as all the slicers will have the same values(if values are the same). Then you can hide the second slicer behind the first slicer.
As you can see Date Table doesn't filter DATA TABLE as I haven't grouped them yet.
Now I got to View in the ribbon and select the sync slicers option and then go to Advanced Controls and group both of the slicers as A.
View >> Sync Slicers >> Advanced Options
Now as both of the slicers are grouped, when I sleect the Year from the Data Table slicer it filters the DATA TABLE table too.
Now just hide the other slicer.
If this answer helped you, then mark it as answer. Thanks.
You should check your relationships.
Make sure you have a date table mapping to your main table.
I've got this table named "A" and I want to fill the Sales SUM column with the sum of sales from another table group by date and country. The other table is named "B" and got also Date, Country and Sales columns however the number of dates and countries differ. I don't want to join this tables I would like to achieve this in DAX. Is it possible?
Yes you can do that with DAX and virtual relationship:
SumSales = calculate( sum('B'[Sales])
, TREATAS(SUMMARIZE('A','A'[Date],'A'[Country]), 'B'[Date],'B'[Country])
)