简体   繁体   中英

How to sort table visualization by column in alphabetical + numerical order?

I have loaded a data into power BI (through 'Get Data') and it looks like this:

在此处输入图像描述

Notice the Subworkload column is properly sorted here "R, W2, W3,....W11" alphabetically and numerically in order.

I just want to do simple thing by showing this table via table visualization in Power BI Report page, but PBI sorted the column in a wrong way. See below:

在此处输入图像描述

How to put it back the properly sorted way?

If it is impossible, can I write python code for visualization to put the table as it is on the report? I just want to do simple thing to present the table on report, but PBI makes it so difficult.

Powerbi is also a microsoft product so I think for sorting similar to Explorer it also uses the API StrCmpLogicalW ( ) for this kind of sorting (called ' natural sort order '). A good explanation can be found here .

import numpy as np
import pandas as pd
import re


df = pd.DataFrame({
    'Subworkload': ['R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9'],
    'Iteration': np.zeros(11),
    'Parameters': ['Buffer_size=Default_Num_Threads=1' for i in range(11)] 
})

def sort_nicely( l ):
    """ Sort the given list in the way that humans expect."""
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    l.sort( key=alphanum_key )

    return l

val = sort_nicely(list(df['Subworkload']))
print(val)

: Output:

'R', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10', 'W11']

But according to StrCmpLogicalW output should be:

'R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM