Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Best Practice for Counting Employees

So I have a dataset of companies and their employee ID's. I'm trying to figure out the best way to count the number of companies by their employee size, based on the number of ID's they have. I curre...
  • Anonymous's avatar
    Anonymous
    8 years ago

    Couple things come to mind:

     

    1) Your SWITCH( TRUE() ) function works well, but it's a little cumbersome.

     

    Try this:

    [Employee Size Range] =
    VAR EmpNum = [Employee Count]
    RETURN
        SWITCH (
            TRUE (),
            EmpNum <= 25, "0-25",
            EmpNum <= 50, "26-50",
            EmpNum <= 100, "51-100",
            EmpNum <= 200, "101-200",
            "200+"
        )

    By creating a variable EmpNum, you don't have to evaluate the [Employee Count] every time.  This will run faster.

    Also, SWTICH() tries each logical comparison in order, so you don't need to use AND.  If it fails the first logical comparison (saying it's less than 25), then by default it will be greater than 25.  No need to check that part again.

     

    Power BI always sorts columns in alphabetical order.  If you ever want to sort columns in a different order, you need to have a column in your data model to sort by.

     

    Good news is, you can use the same template from your measure to create your sorting column:

     

    EmployeeSizeRangeSort =
    VAR EmpNum = [Employee Count]
    RETURN
        SWITCH (
            TRUE (),
            EmpNum <= 25, 1,
            EmpNum <= 50, 2,
            EmpNum <= 100, 3,
            EmpNum <= 200, 4,
            5
        )

    Now, click on the [Employee Size Range] column.  Click the MODELING TAB.  Then Click SORT BY COLUMN.  Click [EmployeeSizeRangeSort]

     

    Hope this helps,


    ~ Chris H