Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sparkymark75
Frequent Visitor

Migrating SQL metrics to Power BI for newbie

I'm new to Power BI and DAX. I'm not from an Excel background either but a SQL one so not overly familiar with a lot of the DAX functions/methods.

 

I have the following SQL query...

 

create table #Metrics
(
dt date,
product varchar(30),
value numeric(9,2),
measure varchar(50)
)

declare @loopstart date
declare @loopend date
declare @thisdate date

set @loopend = dateadd(day, datediff(day, 0, getdate()), 0)
set @loopstart = dateadd(year, -1, dateadd(month, datediff(month, 0, getdate()), 0))

set @thisdate = @loopstart

while (@thisdate <= @loopend)
begin

insert into #Metrics
select 
@thisdate,
x.ITMGEDSC,
count(*),
'MonthFails'
from
(
select
f.IncidentId,
f.createdon,
f.new_completeddate,
f.new_gpitemnumber,
f.new_serialnumber,
i.ACTLSHIP,
f.new_gpcustomerid,
datediff(day, i.ACTLSHIP, f.new_completeddate) daystofail,
i.ITMGEDSC
from 
Failures f join
Installs i on i.ITMGEDSC = left(f.new_gpitemnumber, 6) and f.new_gpcustomerid = i.CUSTNMBR and f.new_serialnumber = i.SERLTNUM
where
f.FailureType = 'Filtered' and
f.new_completeddate between @thisdate and eomonth(@thisdate)

) as x
where
x.ACTLSHIP between dateadd(year, -1, @thisdate) and dateadd(day, -1, @thisdate)
group by
x.ITMGEDSC

insert into #Metrics
select
@thisdate,
x.ITMGEDSC,
count(*),
'Fails'
from
(
select
f.ticketnumber,
f.createdon,
f.new_completeddate,
f.new_gpitemnumber,
f.new_serialnumber,
i.ACTLSHIP,
f.new_gpcustomerid,
datediff(day, i.ACTLSHIP, f.new_completeddate) daystofail,
i.ITMGEDSC
from 
Failures f join
Installs i on i.ITMGEDSC = left(f.new_gpitemnumber, 6) and f.new_gpcustomerid = i.CUSTNMBR and f.new_serialnumber = i.SERLTNUM
where
f.FailureType = 'Filtered' and
f.new_completeddate between dateadd(month, 1, dateadd(year, -1, @thisdate)) and eomonth(@thisdate)
) as x
where
x.ACTLSHIP between dateadd(year, -1, @thisdate) and dateadd(day, -1, @thisdate)
group by
x.ITMGEDSC


insert into #Metrics
select @thisdate dt, ITMGEDSC, count(*) installs, 'Installs' from Installs where ACTLSHIP between dateadd(year, -1, @thisdate) and dateadd(day, -1, @thisdate) group by ITMGEDSC

set @thisdate = dateadd(month, 1, @thisdate)
end;


select i.dt, i.Product, i.value as Installs, isnull(f.value, 0) as Fails, isnull(mf.Value, 0) as MonthFails from #Metrics i left join #Metrics f on i.dt = f.dt and f.measure = 'Fails' and i.Product = f.Product left join #Metrics mf on i.dt = mf.dt and i.Product = mf.Product and mf.measure = 'MonthFails' where i.Measure = 'Installs'

drop table #Metrics

which outputs data like so...

 

dtProductInstallsFailsMonthFails
01/04/2018D209420525
01/04/2018C125815119
01/04/2018M700
01/05/2018D210219816
01/05/2018C128916824
01/05/2018M900
01/06/2018D207319829
01/06/2018C132618426
01/06/2018M1500
01/07/2018D201820025
01/07/2018C132017717
01/07/2018M2821
01/08/2018D205223147
01/08/2018C133518727
01/08/2018M3920
01/09/2018D213024429
01/09/2018C133818211
01/09/2018M5242
01/10/2018D213824939
01/10/2018C136718523
01/10/2018M82157
01/11/2018D219228140
01/11/2018C141820630
01/11/2018M91183
01/12/2018D221828824
01/12/2018C142021640
01/12/2018M92201
01/01/2019D219528632
01/01/2019C145021321
01/01/2019M108266
01/02/2019D204926113
01/02/2019C145420719
01/02/2019M135303
01/03/2019D191422020
01/03/2019C153622530
01/03/2019M155365
01/04/2019D188120421
01/04/2019C161021226
01/04/2019M180382

 

So I've pulled the Failures and Installs tables into Power BI with the aim of replicating the above. I created a new merged query between the Failures and Installs tables called Failures wtih Installs which was efectively a left join between Failures and Installs.

 

In Power query, I created a new calculated column in the Failures with Installs table to determine if the product was installed within 12 months of it failing.

 

if [InWarrantyFails_Installs.ACTLSHIP] >= Date.AddYears([CompletedMonth], -1) and [InWarrantyFails_Installs.ACTLSHIP] <= Date.AddDays([CompletedMonth], -1) then true else false

 

I then created a MonthFails measure in the Failures with Installs table like so;

 

MonthFails = CALCULATE(COUNTROWS('Failures with Installs'),'Failures with Installs'[InWarrantyFail] = TRUE(),GROUPBY('Failures with Installs','Failures with Installs'[CompletedMonth]))
 
This appears to work and gives me numbers which match those in the SQL output for the MonthFails column. What I'm struggling with is the Fails column which is a cumulative total over a date range.
 
I'm not even sure if the way I'm trying to do ALL of this is the correct way. I did start out by basically using my SQL as the query directly. But this limited me when it came to slicing data.
1 REPLY 1
Greg_Deckler
Super User
Super User

Try using the Running Total quick measure in the Desktop. Otherwise, you could also take a look at my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 

Also, if coming from a SQL background, then Phil Seamark's book, Beginning DAX with Power BI is the book for you. https://www.amazon.com/Beginning-DAX-Power-BI-Intelligence/dp/1484234766


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.