Quantcast
Channel: Desktop topics
Viewing all articles
Browse latest Browse all 217217

Inefficient function?

$
0
0

Hi there

 

I wonder if anyone can give me a steer regarding my function code (below).  While it's functional, it cripples the speed of my query.

 

I've a source of Timesheets data where all staff record their time against various projects.  I've no issues obtaining and using the data.  However each staff member's team membership at the time of their timesheet entry is not reliable due to various reasons. 

 

To combat this I have a seperate data source, Staff Movements that simply lists each staff member, their team name and the data on which they joined that team.  That means the table may have multiple records for a staff member, recording their movement through the company.  Eg.

 

Timesheets

Name           Project      Date

Joe Bloggs    Project X   01/01/2014

Joe Bloggs    Project Y   10/04/2015

 

Staff Movements

Name           Team                Date Joined

Joe Bloggs    Project Office    01/01/2014

Joe Bloggs    HR                    10/04/2015

 

My aim is to produce a Timesheets query that includes a custom column, Timesheets.Team that pulls the Staff Movements.Team value where (please excuse the psuedocode) Timesheet.Name = Staff Movements.Namen AND Timesheets.Date >= Staff Movements.Date Joined.

 

I thought I had this licked by creating a power query function that I called from the Timesheets.Team customer column: 

 

fnLookupTeam([Name],[Date],"Team",#"Staff Movements")

 

 

My function:

 

/*  
      Purpose:  Filter the lookup_table by lookup_name and lookup_date, and return the value in the specified column
    */
(lookup_name as text, lookup_date as date, return_column as text, lookup_table as table) as any =>
let
        FilterTable = Table.SelectRows(lookup_table, each Text.Contains([Name], lookup_name) and [Joined] <= lookup_date),
        ReturnResult = 
            if Table.IsEmpty(FilterTable)=true
            then
            "NO TEAM"
            else
            Record.Field(Table.First(Table.Sort(FilterTable,{"Joined",Order.Descending})), return_column)
in ReturnResult#

 

The problem is:  My Timesheets query works blisteringly fast if I omit the function call.  That tells me that data source and my links to it are fine.  When I introduce the function call, the query becomes glacial.  Now I know what I'm asking Power Query to do, and its a bit of logic for each row in a 97,000 query, but I'm a bit surprised that the performance is soooo bad.  As a work around I've simply stood up the two seperate querys (Timesheets and Staff Movements) and performed a DAX query in a customer colum:

 

Team = CALCULATE(values('Staff movements'[team]),TOPN(1,FILTER('Staff movements','Timesheets'[Name]='Staff movements'[Name]&&'Timesheets'[Date]>'Staff movements'[Joined]),'Staff movements'[Joined]))

 

This works incredbibly fast, but means that I cannot use the custom team column in any power queries :-(

 

I'd appreciate anyone's thoughts!

 

Thanks


Viewing all articles
Browse latest Browse all 217217

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>