how do i join two tables using dax - powerbi

i have two tables in power bi that i am trying to join or lookup.
the period table has the days, months, and month id.
the data table has the days and data.
i need to pull the month id from the period table into the data table so that i have a table like the final result picture where the month id is now included.
final result
i tried using lookup and crossjoin but they dont work.

Month = EVALUATE
ADDCOLUMNS (
Sales,
"month", RELATED ( 'period'[day] )
)

Related

How to order date & month column correctly in Power BI?

I have a table that looks like the below photo.
table
I am trying to order Month No and Month Name correctly (starts from January), but the year and month no columns are in Whole number format.
None of the sorting methods is working.
add a calculated column and sort your table by this new column
Year&Month =
'Table'[Year] & FORMAT ( 'Table'[Month No], "00" )

In DAX how do you do current month and previous month aggregation in a multiple used Date table

I need help in DAX. In https://dax.do/?
There is Sales table and Date table with the relationship on Sales.OrderDate = Date.Date
Can you assist to get me this type of answer in DAX:
-- USING SNOWSQL SYNTAX
SELECT
current_date.TargetMonth
,current_date.SalesAmount as SalesAmount
,prev_date.SalesAmount as PrevSalesAmount
(
select date_trunc('MONTH',Date.Date) as TargetMonth, SUM(SalesAmount) as SalesAmount
from Sales
inner Join Date
on Sales.OrderDate = Date.Date
Group by date_trunc('MONTH',Date.Date)
) current_date
INNER JOIN
(
select ADD_MONTHS(date_trunc('MONTH',Date.Date),1) as TargetMonth, SUM(SalesAmount) as SalesAmount
from Sales
inner Join Date
on Sales.OrderDate = Date.Date
Group by ADD_MONTHS(date_trunc('MONTH',Date.Date),1)
) prev_date
on current_date.TargetDate= prev_date.TargetDate
Thank you.

Multiple tables with one calendar table for Power Bi DAX

I have just created a calendar table in my model my power bi has multiple different excel spreadsheets each has their own date field is there a way to connect these all to a calendar table so when using a slicer you only need to reference the calendar table for which month or year you need to filter on?
also how would you fix this line of DAX if i did add a calendar table see how the bottom line is ref a date from the current file would that change when creating a date table?
Monthly Total Actuals =
CALCULATE(
SUM('YTD'[Amount]),
FILTER('YTD','YTD'[Analysis Type] = "ACT"),
FILTER('YTD', 'YTD'[Bill to Project Costing] = "Y"),
FILTER('YTD', 'YTD'[Month Billed].[MonthNo] = MONTH(TODAY()) -1 )
)
thank you so much

Power Bi Sorted Date Table

I have an identified date within a fact table and I need to create a new table that's formatted "MMM-YY" (JAN-01) and sorted in month order.
I entered the data manually from a Excel Spreadsheet with dates ranging from Jan-15 to Dec-30 and sorted by an id num but ideally I want it to update monthly based on a identified date column within the fact table in BI. So for now id want dates upto Sep-19 which would update when the identified dates are refreshed within the fact table.
For now I've used the below to format the column which was inserted manually however id like to change the monthref.date to be based off of an identified date within my fact table.
ID Date = FORMAT('Month Ref'[Date],"MMM-YY")
Id want my date within the fact table to be lookedup/refrenced in another table formatted "mmm-yy" and to be in order of calendar month.
I can use
CALENDAR(MIN(FactEfficiencies[identified_date].[Date]) AND MAX(FactEfficiencies[identified_date].[Date])) to get every date then format but it will bring back the month multiple times when I just need one.
You are already on the good direction with the calendar table. One thing that could help you achieve your result could be including a SUMMARIZE or GROUPBY function. Have a look at this example.
Calendar =
var _fullCalendar =
ADDCOLUMNS (
CALENDAR ( MIN ( 'Table'[Date] ) ; MAX ( 'Table'[Date] ) ) ;
"MonthYear" ; FORMAT ( [Date] ; "MMM-YY" )
)
RETURN
SUMMARIZE ( _fullCalendar ; [MonthYear] )

Combine real and planned data in PowerBI KPI visualizations

I've got two primary datasets:
Real data with all sales transactions.
For example RealData:
Date;Sales
16-01-2017;1200
20-01-2017;1500
05-02-2017;800
08-02-2017;1100
13-02-2017;300
Etc.
A plan with total sales I want to achieve, with totals at the last day of each month.
For example EndOfMonthTargets:
Date;Sales
31-01-2017;3000
28-02-2017;2500
Etc.
In my real data I (a) also have data from the years before 2017, and obviously I have the targets for each month in 2017. The RealData is always live / up to date.
What I'm trying to do is show KPI visualizations: how is the real data vs the plan doing. However, I can't figure out how to relate the two datasets, because the cannot really be joined together.
What is the idiomatic way to do this in PowerBI?
I've tried:
adding a column to each source with its respective status ("real" vs "plan")
creating a union (i.e. "Append Queries as New")
adding a column Month = MONTH([Date])
Giving me this data:
But that's weird, because then the KPI visualization will include the "Plan" data either at the start or worse at the end where it will be plotted as the "current" number.
The expected output is a KPI visualization:
showing the absolute number of sales summed by year (or month, if my slicers are set that way!)
with the Target goals retrieved from the plan data
with a trend based on previous years (not included in my sample data)
How do I achieve that?
There are a few ways to join tables like this. What you have are 2 tables with different granularities: a sales table that goes to the day, and a target table that goes down to the month. It's a common request to model them against each other, and it boils down to having a lookup table that both fact tables can join with.
What I would recommend:
Have a table of your real data
Have a table of your plan/target data, using the last day of the month is fine
Do not relate these 2 fact tables together directly
Rather, have a separate date lookup table, to which both tables are joined. This date dimension should, at a minimum, have a month & year column (e.g. March 2017).
Join both fact tables to your date table
Hide the date fields in your 2 fact tables, so that you aren't tempted to use them in visuals (you have to use the date fields from the lookup table instead)
You can then create measures to SUM your actual and targets. And then additional measures that subtract those 2 measures against each other (or divide one into the other). All those measures will then be sliceable by the month & year on your date table. (You could slice them by date too, but because targets are assigned to the last day of the month, that granularity will be less than helpful.)
There's a lot of good information & examples here: http://www.daxpatterns.com/handling-different-granularities/
The way I would do it:
Add a calculated column to both real and targets tables:
Month = Date(Actual[Date].[Year],Actual[Date].[MonthNo],1)
Month = Date(Target[Date].[Year],Target[Date].[MonthNo],1)
Create relashionships between these
Plot SUM(RealData[Sales]) and Target[Sales] against Target[Month]
You can use PowerQuery to transform data.
"Real" table:
let
Source = Table.FromRecords({[Date="16.01.2017", Sales = 1200], [Date="20.01.2017", Sales=1500], [Date="05.02.2017", Sales = 800], [Date="08.02.2017", Sales = 1100], [Date="13.02.2017", Sales = 300], [Date="10.01.2016", Sales = 1400], [Date="02.02.2016", Sales = 1800]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
"Plan" table:
let
Source = Table.FromRecords({[Date="31.01.2017", Sales = 3000], [Date="28.02.2017", Sales=2500], [Date="31.01.2016", Sales = 2800], [Date="29.02.2016", Sales = 2700]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
Your query for getting data, let's name it GetData:
let
t1 = Table.AddColumn(Real, "Source", each "Real"),
t2 = Table.AddColumn(Plan, "Source", each "Plan"),
MergedTable = Table.Combine({t1, t2}),
AddYear = Table.AddColumn(MergedTable, "Year", each Date.Year([Date])),
AddMonth = Table.AddColumn(AddYear, "Month", each Date.Month([Date])),
Group = Table.Group(AddMonth, {"Year", "Month", "Source"}, {{"Sales", each List.Sum([Sales]), type number}})
in
Group
You may avoid using last step by grouping results at report level, but this will result in users being able to view amount of each sale. If this is expected behavior, just remove Group step, or replace it with Table.RemoveColumn to remove [Date].
If you need separate columns for Plan and Real values, then pivot Source column.
Then just use GetData query in clustered column chart.
You will have to create a measure to calculate the Actual total and measure to calculate the Planned by each month or end of month. Then create a measure to compare the difference between Planned total and Actual total.
Actual =
CALCULATE (
SUM ( RealDate[Sales] ),
FILTER (
ALL ( RealDate ),
MONTH ( [Date] ) = MONTH ( MAX ( EndOfMonthTargets[Date] ) )
&& YEAR ( [Date] ) = YEAR ( MAX ( EndOfMonthTargets[Date] ) )
)
)
Planned = SUM(EndOfMonthTargets[Sales])
ActualVSPlanned = [Actual] - [Planned]
Now use that measures and the Date in eEndOfMonthTargets table to build a matrix:
For the ActualVSPlanned measure use conditional formating as follows:
You will get something like this: