I have a table where i need to count the rows of one column by a certain text string but then subtract the count of another column in that same table
To put it into context. I have a injured person table and i need to count the rows where the injury category = " Lost Time" but i need to deduct the number of injured people where they are a non employee, there is a column in the same table labeled ‘Contractor’ but it is a free text field so i need to identify where there is text in this column and then count the rows to deduct from the total rows counted where Injury Category = Lost Time.
I have used Countrows to calculate the number of Lost Time injuries but i cannot work out how to factor in deducting a count of the Contractor column if there is text in that column, I think i need to add the IF function but im not sure how?
Without a PBIX file it’s difficult to provide a really specific solution, but I think I can provide you a helpful general framework for solving this.
Rather than trying to do this via one complex measure, I would break it into components using the following approach:
Step 1: create a calculated column, using the DAX SEARCH() function to determine row by row if the Contractor column contains the text that would identify that record as a contractor. The final parameter of the SEARCH function lets you set the value if the search string is not found. Set this equal to 0, so if your contractor measure = 0 that means they are an employee.
Step 2: you are off to a good start with your measure approach. Now you just need to modify it as follows to remove the injured who are also contractors:
This is very helpful and i did wonder if i needed to seperate out the measures, my issue is the contractor column is a free text field so all fields in that column that have any type of text i would need to identify in the calculated search column. I can’t attach the file as it has sensitive information but i can attach a screen shot of the column in the table
Yes sorry i should have looked closer, its a free text field so the information is totally dependant on what end users enter however yes we can assume that all entries other than the blank cells and cells with the entry employee are contractors
Thanks Brian really appreciate your time helping to solve this problem for me…I did check out the switch video I try to research and solve the problems myself before coming to the forum but I struggle with the structure of the dax formulas and when to apply them but practise hopefully will make perfect I have a deadline of Friday for this report im working on so you have been a life saver
My pleasure – glad to hear that was what you needed. I don’t know if there’s such a thing as perfect where DAX is concerned, but practice definitely makes progress. Keep at it, and I guarantee the concepts you’re struggling with now will all start to click into place.
Best of luck with your Friday deadline. If you run into any other troubles before then, lots of knowledgeable folks here eager to help.
Do I use the Custom Column function in the query editor to create my calculated column, i tried but a Expression Error message displayed saying"The name “Switch” isn’t recognised. Make sure it is spelled correctly". Should i be creating the calculated column in this way?
No, Query Editor uses a different language (M) and won’t recognize DAX commands. Instead, click on the table you want to add the column to in the Fields view on the right side of your screen, then click the Modeling tab and “New Column”. Then go to the editor window below and paste in your DAX SWITCH code. You may need to click the down arrow (3, on the screenshot below) to see it all, but once you got it entered, hit enter or click the check mark and the calculated column should be added to your selected table. Click on the table view just to check and make sure it calculated correctly.
The calculated column worked in the table but the result produced a 0 for those items where the Contractor column fields were not blank or did not have the text “Employee” i expected those fields to display a 1 in the calculated column. i’ve attached a picture of the calculation and the results