How to find calculated population in dax formula - powerbi

I have a table with country and population for 2017, and I have another table with country and population growth rate%. And I have one table with years like (2018 to 2028). I am trying to find calculated population for 10 years on the basis of these data as we are calculating compound interest.

Because you are working with growth rates, it is very unlikely that you will want to do this calculation as a measure. Rates don't aggregate well.
So, the first thing you're going to want to do is get your data into one table. I would do this in query editor.
You'll need a Cartesian join between your list of countries and a list of years. The PowerBI method for this is a little non-intuitive. You add a custom column, and in the formula you just type in the name of the table.
The result is that every single row in the countries table will be matched with every single row from the years table. If you have 5 rows in one and 10 rows in the other, the resulting table is 50 rows.
Then Merge in your table with the growth rates. Now you have a table that has the name of the country, the 2017 starting population, the growth rate. This set of rows will be repeated for every year from 2018 - 2028.
There is a specific formula for cumulative (compounded) growth.
Pricipal * ( 1 + RatePerPeriod / NumberOfCompoundsPerPeriod) ^ (NumberOfPeriods * NumberOfCompoundsPerPeriod)
You're doing this annually, so it simplifies a bit
Pricipal * ( 1 + Rate) ^ (NumberOfYears)
And the M will look like this:
[2017 Population] * Number.Power((1 + [Growth]),([Year] - 2016))
Good Luck! Hope it helps.

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!

Create a virtual table within a measure. Ignoring the context of rows within a table visulisation

Task: I want to calculate the linear regression (don't worry, you don't need to understand it) of the monthly mileages for a client.
Situation: I have a table containing:
FUM ID (unique asset identifier)
Month
Miles per month extended (interpolated values)
In the summary table visulisation, I have each month and an average of the miles per month.
Miles in each month table
To determine the linear regression, I need:
A table of all the months and miles for each asset.
Only values for the selected client.
The formula I'm using to create the virtual table is
SELECTCOLUMNS (
'Vehicle months table',
"Known[X]", 'Vehicle months table'[Month],
"Known[Y]", 'Vehicle months table'[Miles per month extended]
)
That code keeps the client selection active while it's creating the table which I good.
Now, I'm trying to have each row of the table have the linear regression value of: 1,354
Summary table with linear regression calculation
The rows all vary slightly because for each month in the table, the calculation runs keeping the month context. So when the virtual table is created, it only contains a single month and all the mileages.
I've tried using ALL, ALLSELECTED, SELECTEDCOLUMNS with FILTER. But I can't seem to get the values I'm looking for.
Data model

Date filtering of DAX measure in pivot chart

I have created 3 DAX measures in Power Pivot to calculate backlog value and I have an issue with filtering in pivot chart. When I filter out a specific time period, the backlog calculates only based on filtered dates. Instead of filtering, I would like to only "zoom in" to some specific time-range.
Measures
Opened:=CALCULATE(COUNTA([CreatedOn]))
Closed:=CALCULATE(COUNTA(Ticket[ClosedOn]),USERELATIONSHIP(Ticket[ClosedOn],'Calendar'[Date]))
Backlog:=CALCULATE([Opened]-[Closed],FILTER(ALLSELECTED('Calendar'),'Calendar'[Date]<=MAX('Calendar'[Date])))
Based on example data below, without filtering any dates the backlog value is following:
Date;Backlog
1/1/2021;1
2/1/2021;3
3/1/2021;1
4/1/2021;3
When I filter dates excluding 1st of January, I get following values:
Date;Backlog
2/1/2021;2
3/1/2021;1
4/1/2021;3
I want to get same values as without filtering the date. I just want to to "zoom in" on some specific date range. In my case i have 1 year of input data and want to see the backlog evolution of last 8 weeks. So when I filter last 8 weeks, my first week shows only value calculated for that week, but instead it should take into account all the values from previous periods + the result of first week
Example data
TicketNumber;Type;CreatedOn;ClosedOn;Status
ticket1;Service Request;1/1/2021;1/3/2021;Closed
ticket2;Incident Record;1/2/2021;1/3/2021;Closed
ticket3;Incident Record;1/2/2021;1/5/2021;Closed
ticket4;Service Request;1/4/2021;;Open
ticket5;Service Request;1/4/2021;;Waiting for
Fixed thanks to my colleague! :)
I had to change ALLSELECTED to ALL
Backlog:=CALCULATE([Opened]-[Closed],FILTER(ALL('Calendar'),'Calendar'[Date]<=MAX('Calendar'[Date])))

DAX TopN Behavior

Just wanted to confirm my understanding (or lack thereof) around these two formulas - in an orders table where each row is an order:
TOPN(10,ALL(Orders),[Total Sales]) - looks at the individual Sales amount for each row and returns the whole table with just the top 10 records sorted by the Sales field; using the measure Total Sales(defined as Sum of Sales) in this context doesn't really have an effect as the aggregation is at a single row level which just keeps it the same.
TOPN(10,ALL(Orders[Customer Name]),[Total Sales]) - this actually groups by the customer name, calculates the total sales, and returns the top 10 customer names based on that metric; it's more or less equivalent to this SQL:
select customer_name, sum(sales) as Total_Sales from orders
group by customer_name
order by Total_Sales desc
limit 10

DAX CALCULATE() doesn't seem to be overriding pivot filter

I am learning to use Power Pivot and write DAX and I am working through a book I bought written by Rob Collie and Avichal Singh called "Power Pivot and Power BI".
Early on it explains what happens when using CALCULATE() in a measure. A key point of understanding is explained as follows:
If a filter argument acts on a column that IS already on the pivot, it
will override the pivot context for that column
So in a simple table called "GSR" I have a series of invoices with an invoice date, a product and an amount. I have another column that converts all invoice dates to the last day of the month to gather them together. I have created a measure called "Total orders" that is counting the number of rows.
I have created a pivot of this data with:
Products in the rows
Month end in the columns (but I've actually chosen Month and Year from the automatic breakdown Power Pivot has done on my Month_End column)
Orders in the values
This pivot renders correctly.
Now the issue:
I've created two slicers that are feeding off two disconnected tables; one containing month numbers, and one containing years. Based on the selection from each of these I have a measure that creates a month end using the EOMONTH(DATE(),0). This measure is called "Comparison_Month_End".
I then have another measure called "Compare_Orders" that contains the following:
CALCULATE([Total orders],FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]))
The point of all of this - this is meant to get the orders from the GSR where the month and year match the slicer selection. I want this value to appear in the pivot for every month selected i.e. not filtered by the pivot.
It doesn't work, however. This seems to me to be counter to what the book says, which is that if the filter in the measure is applied to a column on the pivot (in this case Month_End), the pivot filter is overriden. So, for example, if I have 31-Mar-18 in a column on my pivot and the comparison month end is 31-Jan-18, I would expect to see the January orders within "Compare_Orders" sitting next to the March orders, the February orders, the December orders and so on, but it only appears next to January.
To me this is doing exactly the same as an example in the book where there is a pivot with Year in the rows, Total Sales in column 1 and then 2002 sales in column 2. Column 1 shows sales for each year, column 2 shows the same sales figure (i.e. 2002 sales) against every row, even where the row year is 2001, 2003 or 2004. The 2002 Sales measure is using CALCULATE() summing Total Sales, filtering on "Sales[Year] = 2002".
Could anyone please explain why what I expected to happen doesn't seem to be happening please?
Thank you.
CALCULATE does indeed override the filter context when evaluating its first argument; using the rows provided to its second argument as the new filter context. However; CALCULATE does not alter the filter context when evaluating its second argument, and so pre-existing filters remain unless they are explicitly removed.
In FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]), FILTER only iterates over rows of GSR that are present in the pre-existing filter context (the pivot context). To get all rows, use ALL:
CALCULATE([Total orders],FILTER(ALL(GSR),GSR[Month_End]=[Comparison_Month_End]))