PowerBI - Calculate average year from a date column - powerbi

I have a table (we'll just call it MyTable) in PowerBI that includes a Date column. I have created a DateDimension table using CALENDARAUTO() with columns for the Date, Month#, MonthName, etc and created the relationship between that Date and the Date in MyTable.
I want to calculate the average year based on the records in MyTable. For example, if I have records with 1/1/2005, 1/1/2014, and 1/1/2015, the average year should be 2011.
How would I go about doing that? I've tried doing a measure of AVERAGE(DateDimension[Year]) and adding that to a Card visual, but it just shows 2.01K. If I do FORMAT(AVERAGE(DateDimension[Year]), "####"), the average is completely wrong.

Since you already have this DateDimension table, use
Average =
AVERAGEX(
MyTable,
RELATED(DateDimension[Year])
)
You can control the formatting by clicking on the measure (Average) and then using the Measure tools pane:
Set it to Whole number and no thousands operator.

Related

calculate an average on a calculated measure in DirectQuery

I'm using Power BI, in directQuery mode for all my tables (other than my date table).
I can't change that.
To simplify, I have a fact table that has an id, a date field and a sales figure (not even using the sales for this question.. just the fact that there is a sale on this date).
The date field relates to my date dimension as you'd expect.
I have created a measure, which is a rolling 2 year count of sales (not sum, just count).
Rolling_year_sales_count = CALCULATE(
DISTINCTCOUNT( fact[sale_id]),
DATESINPERIOD(
dim_date[yyyy_mm_dd],
LASTDATE(dim_date[yyyy_mm_dd]),
-2,
Year
))
this looks good when I put it on a simple table - its properly counting the rolling year counts.
Now, I want to create a rolling 2 year AVERAGE count.
So the result should show 2 for 1925, 5 for 1926, 8 for 1927... (Add the 2 previous year's rolling counts, and divide by 2)
I tried just including this measure in a new measure that uses SUMMARIZE - but because I am using DirectQuery, I cannot refer to calculated measures in my SUMMARIZE.. Same goes for CALCULATE.
So how can I calculate this average, knowing that I'm in directQuery mode?
Thanks!

Finding Previous year Sales in Power BI with week number

I am trying to find the Previous year sales for the same week in Power BI.I dont have any date column.
I have two table one is FACT Indicators table as shown below:
and one sales table( Fact Sales table):
I want to create one calculated field namely(Sales Previous Year) to show the previous year sales for the same week .
In 'Fact Indicators' table 'PY 52 week flag' filed shows if this week id is Previous year or not.
Week column shows the week number from 1 to 52 weeks .
Week Id shows the unique number per Market key.
'Market_Week Id Key' is the common joining key between FACT Indicators table and Fact Sales table
Please help me to find the formula for calculated field.I dont have the date field in my raw data
Every time you deal with anything related to dates, you will need to add what we call a date dimension. It will save you tons of headaches. Once you have it in you will be able to hook it into the creation of the calculated filed.
you can google power bi or ssas date dimension and find tons of information on it.
Yeah! I guess SQL Technical team can be a tough crowd.... Well! In this case, I would recommend bringing the Year into FactSales Table from Fact Indicator . You have two options here with physical relationship set up between Market Week Id Key in both tables you can build a calc column with
Year = CALCULATE(VALUES(FactIndicators[Year]))
or without relationship use LOOKUPVALUE on WeekId
Year = LOOKUPVALUE(FactIndicators[Year], FactIndicators[WeekId], FactSales[WeekId])
Sales Last Year calc colum :
SalesLastYear =
CALCULATE (
SUM(FactSales[SalesThisYear] ),
TOPN(1,
FILTER(
FactSales,
FactSales[Year] < EARLIER(FactSales[Year])
&& FactSales[Key] < EARLIER(FactSales[Key])
)
)
)

Calculate Works until i add FILTER

I'm learning DAX and I'm trying to make a measure for Sales Last year.
this formula works :
Sales LY = CALCULATE([Sales CY],SAMEPERIODLASTYEAR(Date[Date]))
But when I add FILTER to the measure, it gives me the values from the current year like "Sales CY"
Sales LY = `CALCULATE([Sales CY], Filter (dim_date, SAMEPERIODLASTYEAR (Date[Date]) ))`
I already have a date filter on the page relative date in this year
the invoices Table and Date table are joined on the date of the creation of the invoice (createdat)
Any ideas what's the meaning behind the things in blue circles?
The FILTER function expects a condition that can be checked for each row of the table instead of a column of dates returned by SAMEPERIODLASTYEAR, so I wouldn't expect the second version to work.
I think the bits circled in blue are emphasizing that you are connected an imported data table to a DirectQuery table (different storage modes).

Power BI Sum by Category and Month

I have a Power BI/DAX question. I'm looking to summarize my data by getting monthly transaction sums (including the year as well, i.e. MM/YY) and filtering them by individual account numbers. Here is an example:
I want to take that and make it into this:
I converted the dates to the format I want with this code: 
Transaction Month = MONTH(Table[Date]) & "/" & YEAR(Table[Date])
Then got the total monthly sum:
Total Monthly Sum = CALCULATE(sum(Table[Transaction Amount]),ALLEXCEPT(Table, Table[Transaction Month]))
Now I'm trying to figure out how to filter the total monthly sum by individual account numbers. Just as a note - I need this to be a calculated column as well because I'll want to identify accounts that surpass individual account monthly spending limits. Can anyone help me with this?
Thanks so much!
When working with calendar dates, it pays to have a calendar table linked to the transaction table. In the calendar table you will have each date, from the start date of your relevant time period to the end of the time period relevant to your data. The columns of the calendar table can then contain calculations on that date like month number, month name, year, year-month key, transaction month (as the first day of the month for the date in that row), etc.
Next, connect the two tables in the data model by dragging the transaction date to the calendar date column.
Now you can build charts and report tables that group data by month without writing any complicated DAX. Just pull the field "transaction month" from the calendar table and the Total Sum measure from the transaction table into the field well of the visual.
That's what Power BI is all about.

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.