Power Query M Question - Time in Pod/training

KCantor

New member
Joined
Apr 4, 2016
Messages
15
Reaction score
0
Points
0
I have ordered the book on M but still haven’t learned enough to be of any use. So, here I am again on the board asking for help making powerquery do my bidding.
I have a ‘Pod Members” table that I need to create a couple of calculations on. It has true duplicate entries that I need to remove in order to make the pivot table work but I don't know how to automatically judge which entry to keep. I also need to calculate days in pod, days in training, and, using that calculation create pod memberships.
I have attached a modified version of the table here. The names have been changed to protect the innocent.
Steps I need to create:
1.) Determine current pod membership. If a team member is currently in the pod, the end date will be 12/31/9999. Remove/hide duplicate member names that have left a pod. In the attached table there should be 3 examples of those duplicates. Of course the next step may need part of the information from the deleted entries.
2.) Determine if the member is in training. A member is in training if their start date is less than 45 days from the current date. After 45 days, they are no longer in training and count in calculations as a pod member. MSTARTED is not a new hire/ trainee but has switched to a new pod in the example.
3.) Create a method of calculating pod current pod membership to be used as a divisor in sales calculations. I need to be able to divide sales calls and sales dollars by the number of current pod members who are out of training. So, if a member is new, their calls and sales dollars count but the divisor is different. For example, JLENON is in the Sales Ninjas pod but has not been with us for 45 days. Therefore, the Sales Ninjas pod membership count should be 2, not 3 until his training is complete.
4.) As a wish list item, I would like to add a new column calculating the number of days spent in the new pod in case someone switched pods in the middle of a calculation period. My fear on this is how to keep them in two pods without creating duplicate entries that would negate table relationships. As a way to split sales calls and sales dollars between two pods if they changed to another.
As always, I appreciate your help and the sharing of your wisdom.

SalesPersonCodeFirst Training DateTopPodCodeStart DateEnd DateTopPodNameid
ALINCOLN9/23/2014 0:00TT1/1/2015 0:0012/31/9999 0:00Team Troll99
BCAUGHT10/27/2015 0:00BP2/6/2016 0:0012/31/9999 0:00Bro Pod57
BCAUGHT10/27/2015 0:00SJ10/27/2015 0:002/5/2016 0:00Sales Jedis58
FPAPER11/10/2015 0:00SJ11/10/2015 0:002/22/2016 0:00Sales Jedis108
GWASHINGTON1/5/2016 0:00BP1/5/2016 0:0012/31/9999 0:00Bro Pod109
JBEIBER2/17/2016 0:00SJ2/17/2016 0:0012/31/9999 0:00Sales Jedis261
JCASTILLION4/15/2014 0:00BP1/1/2015 0:001/12/2016 0:00Bro Pod317
JCASTILLION4/15/2014 0:00DP1/13/2016 0:0012/31/9999 0:00Digital Pod318
JLENON2/25/2016 0:00SN2/25/2016 0:0012/31/9999 0:00Sales Ninjas86
JOCEAN6/30/2015 0:00TT6/30/2015 0:0012/31/9999 0:00Team Troll141
KPEAN1/6/2011 0:00SN1/1/2015 0:0012/31/9999 0:00Sales Ninjas56
MSTARTED8/13/2013 0:00BC1/1/2015 0:004/1/2016 0:00Big Cheese205
MSTARTED8/13/2013 0:00DP4/2/2016 0:0012/31/9999 0:00Digital Pod206
NFASHION5/6/2013 0:00TT1/1/2015 0:0012/31/9999 0:00Team Troll222
OOSBORNE9/8/2015 0:00BC9/8/2015 0:0012/31/9999 0:00Big Cheese245
PFRAMPTON9/28/2010 0:00BC1/1/2015 0:0012/31/9999 0:00Big Cheese61
PMCCARTNEY8/11/2015 0:00SN8/11/2015 0:0012/31/9999 0:00Sales Ninjas81
STYLER3/18/2014 0:00BP1/1/2015 0:0012/31/9999 0:00Bro Pod78
TWEET5/27/2014 0:00BC1/1/2015 0:009/7/2015 0:00Big Cheese305
TWEET5/27/2014 0:00SJ9/8/2015 0:0012/31/9999 0:00Sales Jedis306
 

Attachments

  • podcalc.xlsx
    123.4 KB · Views: 23
Let's try this to get started:

1) Filter out the "End Date" records that don't equal 12/31/9999 (Based on your sample data, this gives you only people still in the pod)

2) To work out the InTraining status:
  • Go to Add Column --> Add Custom Column
    • Name: Today's Date
    • Formula: =DateTime.LocalNow()
  • Select the Today's Date column, hold down CTRL, select the First Training Date column
  • Go to Add Column --> Date --> Subtract Days
  • Go to add Column --> Add Custom Column
    • Name: InTraining
    • Formula:
Code:
if [DateDifference]<45then "In Training"
else null


  • Select the Today's Date and DateDifference columns and remove them

To be fair, this could be done in less steps, but I wanted you to drive the user interface way to accomplish this.

Let's save your query as a connection:
  • Change the Query name to "Base"
  • Go to Home --> click the bottom of the Close & Load button --> Close & Load To... --> Only Create Connection

You now have a pointer to the data set with the current pod employees.

3) Let's work out how many people are in each pod
  • In the Workbook Queries pane, right click the "Base" query --> Reference
  • Filter the InTraining column --> uncheck the "In Training" value (this leaves only full fledged staff)
  • Go to Transform --> Group By
    • Group By: TopPodName
    • New column name: CountOfPeople
    • Operation: Count Rows

This gives you a table showing only the count of the employees. (If you had sales data in that same table, we could have grouped it here too.)

Let's save your query as a connection:
  • Change the Query name to "PeoplePerPod"
  • Go to Home --> click the bottom of the Close & Load button --> Close & Load To... --> Only Create Connection

I know that doesn't get you to point 4 yet, and the data is not loading anywhere, but does this makes sense so far?
 
Okay, I am with you and now have both references built in. First off, thank you very much for assisting me and second, WOW, I think this tool is going to become my favorite.
It is making perfect sense and bringing clarity to an entire plethora of questions. Please continue!
 
I am loading this into PowerPivot and, eventually, PowerBI to create dashboards. The end goal is to tie sales to calls and reps.
 
Okay, so here's the deal. What I figured is probably the best idea here is to add the Pod to the Sales Fact table, as the pod is time dependent. To do that, we create a custom function (chapter 21 of the book).

  • Right click the Base query to create a referenced query
  • Change the type of the Start Date column to Date (from DateTime)
  • Sort the Start Date column in descending order (bringing the newest record to the top)
  • Filter the SalesPerson column to anyone. (I used Castillo since this returns multiple results)
  • Filter the Start Date column to Before or Equal To any date you choose
  • Right click the Pod Name in the first row and choose Drill Down

We've now done the leg work for the query, we need to make it dynamic. Go into the Advanced Editor and make the following changes:

  • Add the following line at the top:
Code:
(salesperson as text, saledate as date) =>

  • Now modify the Filtered Rows lines so that our variables are in there:
Code:
    #"Filtered Rows" = Table.SelectRows(#"Sorted Rows", each ([SalesPersonCode] = [COLOR=#ff0000]salesperson[/COLOR])),    
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [Start Date] <= [COLOR=#ff0000]saledate[/COLOR]),


  • Click Done
  • Change the query name to fxGetPod

You're now set to use it...
  • Create a new query that references the SalesPerformance table
  • Go to Add Column --> Add Custom Column
    • Column Name: Pod
    • Formula: fxGetPod([SalesPerson],[OrderDate])

And that should do it.
 

Attachments

  • podcalc.xlsx
    141.9 KB · Views: 18
I will be trying to finish this today. My book is actually due in tomorrow so hopefully between this answer and the book I will get this working. If not, I will be back here with remarks. I didn't want you to think I didn't appreciate your help. Just taking me a little longer due to work and inexperience.
 
Back
Top