Ranking

Barras de ranking

¿Qué subcategorías venden más y cuánto pesan en el total?

Top 10 subcategorías por ventas de Contoso 2024: Toys and Kids primera con 1,18 M$, Video Game Consoles y Computers detrás; las tres primeras suman el 32 % de las ventas.

Captura en Power BI Desktop sobre el modelo Contoso-Retail, año 2024.

Las diez subcategorías que más venden, con su puesto en un círculo, barras redondeadas en degradado frente a la líder (las tres primeras resaltadas), el importe y su parte del total. El subtítulo dice cuánto suman las tres primeras.

Qué necesita

  • Una columna de subcategoría (o la que quieras ordenar)
  • Una medida de ventas

Cómo lo conecto a mis datos

  1. Copia la medida de abajo y crea una medida nueva con ella.
  2. Cambia 'DimProduct'[SubCategoryName] por tu columna y [Total Sales] por tu medida.
  3. Pon su categoría de datos en «URL de imagen» (Herramientas de medidas → Categoría de datos).
  4. Añade un visual Imagen → Formato → Imagen → «Seleccionar desde datos» → la medida, y filtra un año en la página.

La medida completa

Es la misma que el archivo .dax de la carpeta. Cópiala con el botón.

barras-ranking.dax
-- SVG measure for Power BI: set its data category to Image URL and show it in an Image visual (Format > Image > Select from data).
-- Uses the Contoso-Retail master: DimProduct columns and [Total Sales], [Sales PY], [Total Quantity], [Margin %].
SVG Barras Ranking =
-- Top 10 subcategories by sales: rank badge, gradient bar on a track, value and share
VAR _yr = SELECTEDVALUE ( 'DimDate'[Year] )
VAR _t = TOPN ( 10, ADDCOLUMNS ( VALUES ( 'DimProduct'[SubCategoryName] ), "@v", [Total Sales] ), [@v], DESC )
VAR _r = ADDCOLUMNS ( _t, "@r", RANKX ( _t, [@v], , DESC, DENSE ) )
VAR _max = MAXX ( _t, [@v] )
VAR _tot = [Total Sales]
VAR _top3 = DIVIDE ( SUMX ( TOPN ( 3, _t, [@v], DESC ), [@v] ), _tot )
VAR _rows =
    CONCATENATEX (
        _r,
        VAR _y = 118 + ( [@r] - 1 ) * 52
        VAR _w = MAX ( DIVIDE ( [@v], _max ) * 600, 24 )
        VAR _lead = [@r] <= 3
        RETURN
            "<g transform='translate(0," & _y & ")'>"
                & "<circle cx='64' cy='18' r='16' fill='" & IF ( _lead, "url(#gb)", "#EEF2F7" ) & "'/>"
                & "<text x='64' y='23.5' text-anchor='middle' font-size='15' font-weight='700' fill='" & IF ( _lead, "#FFFFFF", "#1F3A5F" ) & "'>" & [@r] & "</text>"
                & "<text x='96' y='24' font-size='17' fill='#1F2937'" & IF ( _lead, " font-weight='600'", "" ) & ">" & SUBSTITUTE ( 'DimProduct'[SubCategoryName], "&", "&amp;" ) & "</text>"
                & "<rect x='370' y='5' width='600' height='26' rx='13' fill='#EEF2F7'/>"
                & "<rect x='370' y='5' width='" & FORMAT ( _w, "0.0", "en-US" ) & "' height='26' rx='13' fill='" & IF ( _lead, "url(#gb)", "url(#gl)" ) & "'/>"
                & "<text x='" & FORMAT ( 370 + _w + 12, "0.0", "en-US" ) & "' y='24' font-size='16' font-weight='600' fill='#1F3A5F'>$" & FORMAT ( [@v] / 1e6, "0.00", "en-US" ) & "M</text>"
                & "<text x='" & FORMAT ( 370 + _w + 84, "0.0", "en-US" ) & "' y='24' font-size='14' fill='#6B7A90'>" & FORMAT ( DIVIDE ( [@v], _tot ), "0%", "en-US" ) & "</text>"
                & "</g>",
        "",
        [@r], ASC
    )
RETURN
    "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1200 650' width='1200' height='650' font-family='Segoe UI, sans-serif'>"
        & "<defs><linearGradient id='gb' x1='0' x2='1'><stop offset='0' stop-color='#1F3A5F'/><stop offset='1' stop-color='#2E6BA0'/></linearGradient>"
        & "<linearGradient id='gl' x1='0' x2='1'><stop offset='0' stop-color='#7395C0'/><stop offset='1' stop-color='#A3B8D6'/></linearGradient></defs>"
        & "<text x='48' y='48' font-size='26' font-weight='600' fill='#1F3A5F'>Top 10 subcategorías por ventas</text>"
        & "<text x='48' y='80' font-size='16' fill='#6B7A90'>" & _yr & " · las tres primeras suman el " & FORMAT ( _top3, "0%", "en-US" ) & " de las ventas · barra frente a la líder</text>"
        & _rows
        & "</svg>"

El informe lee los datos de muestra por internet: al abrirlo la primera vez, pulsa Actualizar. El visual responde a los filtros de la página. Licencia MIT.

¡Contáctame!