- Inicio
- Recursos
- Galería Deneb
- Sankey de ventas: canal → segmento de cliente → categoría
Sankey de ventas: canal → segmento de cliente → categoría
¿Qué clientes sostienen cada categoría y por qué canal llegan? Cada flujo es una parte de la venta que va del canal al segmento de cliente y de ahí a la categoría que compra. En Contoso, los clientes de valor alto se llevan la mitad de Electronics, y los de valor bajo compran sobre todo música, cine y juguetes.
💡 Cuándo usarlo
Cuando la venta pasa por tres clasificaciones y quieres ver cómo se reparte de una a otra: canal → segmento → categoría, origen → campaña → producto. Cada etapa se lee bien hasta unos 8 valores; con más, agrupa los pequeños en «Otros» antes. Si solo tienes dos dimensiones, una matriz con mapa de calor es más fácil de leer.
Vista previa en vivo
Render con datos de muestra · Vega/Vega-Lite🖱️ Interactivo — haz clic o arrastra sobre las marcas y el gráfico responde aquí mismo.
Cargando vista previa… Vista previa renderizada con el runtime de Deneb · MIT © Daniel Marsh-Patrick
¿Te aviso cuando salga un visual nuevo?
Opcional: la descarga no lo necesita. Un correo cada dos semanas como mucho. Y si quieres todos los visuales de pago, está el Pase Deneb.
🧩 Campos que espera
-
__0__Channel texto -
__1__Customer Segment texto -
__2__CategoryName texto -
__3__Total Sales número medida
Arrastra tus campos a Values en este orden.
🎯 Técnica
- Motor
- Vega 5.30.0
- Transforms
- aggregate agregación, filter filtro, formula fórmula calculada, joinaggregate agregación por grupo, lookup, window ventana (acumulados/ranking)
- Parámetros
-
-
UI_Stages: las etapas, de izquierda a derecha: cada una con su título (label) y el campo que lee (field); el título pinta la cabecera y el campo decide qué filtra el clic se cambia en el spec
Y 15 señales internas de cálculo y diseño.
-
- Power BI
-
-
pbiContainerHeight: alto del recuadro del visual, para que el gráfico se ajuste -
pbiContainerWidth: ancho del recuadro del visual, para que el gráfico se ajuste -
pbiCrossFilterApply: filtra el resto del informe con lo que se pulsa -
pbiCrossFilterClear: quita el filtro cruzado del resto del informe -
pbiFormatAutoUnit: abrevia un número grande con su unidad (K, M…), como hace Power BI
-
🛠️ Cómo está hecho
Vega puro, sin transformación de Sankey: tres aggregate dan los nodos de cada etapa, se ordenan por venta con window y se apilan con la misma escala de alto. Los flujos se apilan dentro de su nodo de origen y de destino con otro window, y linkpath los dibuja como cintas. Al pasar el cursor por un nodo se resaltan sus flujos. Las etapas son una sola lista de objetos, UI_Stages, con el título y el campo de cada una: una sola marca de texto pinta las tres cabeceras y el clic filtra por el campo de la etapa. Un clic en un nodo filtra el informe por ese canal, segmento o categoría, y una línea al pie nombra el flujo mayor. La vista previa usa Contoso Retail 2023–2024.
Cómo usarlo en Power BI
- Agrega el visual Deneb a tu reporte e ingresa a su editor.
- Arrastra tus campos a Values en este orden: Channel , Customer Segment , CategoryName , Total Sales .
- Pega el spec (botón «Copiar spec» arriba) en el editor JSON de Deneb.
- Mapea tus columnas y ajusta a tu gusto. La interactividad nativa ya viene configurada.
Ver el spec Vega completo
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "Sankey de tres etapas: sigue cada peso de venta desde el canal, pasando por el segmento de cliente, hasta la categoría que compra. Responde qué clientes sostienen cada categoría y por qué canal llegan. Por CSalcedoDataBI · https://csalcedodatabi.com/deneb/sankey-canal-segmento-categoria",
"usermeta": {
"deneb": {
"build": "1.9.0",
"metaVersion": 1,
"provider": "vega",
"providerVersion": "5.30.0"
},
"interactivity": {
"tooltip": true,
"contextMenu": true,
"selection": true,
"highlight": false,
"dataPointLimit": 100,
"selectionMode": "advanced"
},
"information": {
"uuid": "0b0763fc-c0a7-4e64-bb16-275d7235562b",
"generated": "2026-12-01T05:33:52.822Z",
"name": "Sankey de tres etapas",
"description": "Sales Sankey: Channel → Customer Segment → Category. Pure Vega, no custom DAX. Ficha y vista previa en vivo: https://csalcedodatabi.com/deneb/sankey-canal-segmento-categoria. Autor: Cristobal Salcedo Beltrán.",
"author": "Cristobal Salcedo Beltrán"
},
"dataset": [
{
"key": "__0__",
"name": "Channel",
"description": "Primera etapa (izquierda): canal, origen, campaña…",
"type": "text",
"kind": "column"
},
{
"key": "__1__",
"name": "Customer Segment",
"description": "Etapa central: segmento, región, vendedor…",
"type": "text",
"kind": "column"
},
{
"key": "__2__",
"name": "CategoryName",
"description": "Última etapa (derecha): categoría, producto, destino…",
"type": "text",
"kind": "column"
},
{
"key": "__3__",
"name": "Total Sales",
"description": "Medida que da el grosor de cada flujo. Debe ser positiva.",
"type": "numeric",
"kind": "measure"
}
]
},
"width": {
"signal": "pbiContainerWidth"
},
"height": {
"signal": "pbiContainerHeight"
},
"padding": 0,
"autosize": {
"type": "none",
"contains": "padding"
},
"config": {
"text": {
"font": "Segoe UI, Inter, -apple-system, Helvetica Neue, sans-serif"
}
},
"signals": [
{
"name": "UI_Stages",
"description": "The stages, left to right: one object per stage with its header (label) and the dataset field it reads (field). The header row, the stage nodes and the cross-filter all read this list.",
"value": [
{
"label": "Canal",
"field": "__0__"
},
{
"label": "Segmento de cliente",
"field": "__1__"
},
{
"label": "Categoría",
"field": "__2__"
}
]
},
{
"name": "LAYOUT_NodeWidth",
"description": "Width of each node bar, in px.",
"value": 14
},
{
"name": "LAYOUT_NodePadding",
"description": "Vertical gap between two nodes of the same stage, in px.",
"value": 14
},
{
"name": "LAYOUT_HeaderHeight",
"description": "Height of the stage header row, in px.",
"value": 26
},
{
"name": "LAYOUT_FooterHeight",
"description": "Height of the bottom row that holds the annotation of the largest flow, in px.",
"value": 24
},
{
"name": "LAYOUT_LabelLeft",
"description": "Room for the labels of the first stage, left of its nodes.",
"update": "clamp(width * 0.13, 80, 130)"
},
{
"name": "LAYOUT_LabelRight",
"description": "Room for the labels of the last stage, right of its nodes.",
"update": "clamp(width * 0.23, 120, 220)"
},
{
"name": "LAYOUT_PlotHeight",
"description": "Height left for the nodes once the header and the footer are taken.",
"update": "max(40, height - LAYOUT_HeaderHeight - LAYOUT_FooterHeight - 6)"
},
{
"name": "DATA_Total",
"description": "Total of the measure over every flow: the base of every share.",
"update": "data('totals').length ? data('totals')[0].grand : 1"
},
{
"name": "DATA_MaxNodes",
"description": "Node count of the stage with the most nodes: it sets how much padding the scale leaves.",
"update": "data('stageMax').length ? data('stageMax')[0].nMax : 1"
},
{
"name": "INTERNAL_PixelsPerUnit",
"description": "Pixels per unit of the measure, shared by every stage so the three columns stack to the same height.",
"update": "max(0, (LAYOUT_PlotHeight - (DATA_MaxNodes - 1) * LAYOUT_NodePadding) / max(DATA_Total, 1e-9))"
},
{
"name": "LAYOUT_StageX",
"description": "Left x of the nodes of each stage (first, middle, last).",
"update": "[LAYOUT_LabelLeft, (LAYOUT_LabelLeft + width - LAYOUT_LabelRight - LAYOUT_NodeWidth) / 2, width - LAYOUT_LabelRight - LAYOUT_NodeWidth]"
},
{
"name": "STATE_AnySelected",
"description": "True when a click on a node selected rows (Deneb __selected__): the flows outside the selection dim.",
"update": "data('totals').length ? data('totals')[0].selRows > 0 : false"
},
{
"name": "STATE_HoverNode",
"description": "Key of the node under the pointer: its flows light up and the rest fade.",
"value": null,
"on": [
{
"events": "@node:pointerover, @nodeLabel:pointerover",
"update": "datum.key"
},
{
"events": "@node:pointerout, @nodeLabel:pointerout",
"update": "null"
}
]
},
{
"name": "STATE_HoverLink",
"description": "Key of the flow under the pointer: it is drawn stronger than the rest.",
"value": null,
"on": [
{
"events": "@link:pointerover",
"update": "datum.linkKey"
},
{
"events": "@link:pointerout",
"update": "null"
}
]
},
{
"name": "STATE_CrossFilter",
"description": "Click on a node filters the report by that node; a click elsewhere clears it.",
"value": [],
"on": [
{
"events": {
"source": "scope",
"type": "mouseup",
"markname": "node"
},
"update": "pbiCrossFilterApply(event, \"datum['\" + UI_Stages[datum.stage].field + \"'] == '\" + datum.name + \"'\")"
},
{
"events": {
"source": "scope",
"type": "mouseup",
"markname": "nodeLabel"
},
"update": "pbiCrossFilterApply(event, \"datum['\" + UI_Stages[datum.stage].field + \"'] == '\" + datum.name + \"'\")"
},
{
"events": {
"source": "view",
"type": "mouseup",
"filter": [
"!event.item || (event.item.mark.name != 'node' && event.item.mark.name != 'nodeLabel')"
]
},
"update": "pbiCrossFilterClear()"
}
]
}
],
"data": [
{
"name": "dataset"
},
{
"name": "stages",
"transform": [
{
"type": "sequence",
"start": 0,
"stop": {
"signal": "length(UI_Stages)"
},
"as": "stage"
}
]
},
{
"name": "rows",
"source": "dataset",
"transform": [
{
"type": "formula",
"as": "s1",
"expr": "'' + datum[UI_Stages[0].field]"
},
{
"type": "formula",
"as": "s2",
"expr": "'' + datum[UI_Stages[1].field]"
},
{
"type": "formula",
"as": "s3",
"expr": "'' + datum[UI_Stages[2].field]"
},
{
"type": "formula",
"as": "v",
"expr": "max(0, toNumber(datum['__3__']) || 0)"
},
{
"type": "formula",
"as": "sel",
"expr": "datum.__selected__ === 'on' ? 1 : 0"
},
{
"type": "filter",
"expr": "datum.v > 0"
}
]
},
{
"name": "totals",
"source": "rows",
"transform": [
{
"type": "aggregate",
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"grand",
"selRows"
]
}
]
},
{
"name": "n1",
"source": "rows",
"transform": [
{
"type": "aggregate",
"groupby": [
"s1"
],
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"value",
"selN"
]
},
{
"type": "formula",
"as": "name",
"expr": "datum.s1"
},
{
"type": "formula",
"as": "stage",
"expr": "0"
}
]
},
{
"name": "n2",
"source": "rows",
"transform": [
{
"type": "aggregate",
"groupby": [
"s2"
],
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"value",
"selN"
]
},
{
"type": "formula",
"as": "name",
"expr": "datum.s2"
},
{
"type": "formula",
"as": "stage",
"expr": "1"
}
]
},
{
"name": "n3",
"source": "rows",
"transform": [
{
"type": "aggregate",
"groupby": [
"s3"
],
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"value",
"selN"
]
},
{
"type": "formula",
"as": "name",
"expr": "datum.s3"
},
{
"type": "formula",
"as": "stage",
"expr": "2"
}
]
},
{
"name": "nodes",
"source": [
"n1",
"n2",
"n3"
],
"transform": [
{
"type": "formula",
"as": "key",
"expr": "datum.stage + '|' + datum.name"
},
{
"type": "window",
"groupby": [
"stage"
],
"sort": {
"field": [
"value",
"name"
],
"order": [
"descending",
"ascending"
]
},
"ops": [
"sum",
"row_number"
],
"fields": [
"value",
null
],
"as": [
"cum",
"rank"
],
"frame": [
null,
0
]
},
{
"type": "joinaggregate",
"groupby": [
"stage"
],
"ops": [
"count"
],
"as": [
"n"
]
},
{
"type": "formula",
"as": "offset",
"expr": "LAYOUT_HeaderHeight + max(0, (LAYOUT_PlotHeight - (DATA_Total * INTERNAL_PixelsPerUnit + (datum.n - 1) * LAYOUT_NodePadding)) / 2)"
},
{
"type": "formula",
"as": "y0",
"expr": "datum.offset + (datum.cum - datum.value) * INTERNAL_PixelsPerUnit + (datum.rank - 1) * LAYOUT_NodePadding"
},
{
"type": "formula",
"as": "y1",
"expr": "datum.y0 + datum.value * INTERNAL_PixelsPerUnit"
},
{
"type": "formula",
"as": "x0",
"expr": "LAYOUT_StageX[datum.stage]"
},
{
"type": "formula",
"as": "share",
"expr": "datum.value / DATA_Total"
},
{
"type": "formula",
"as": "colorKey",
"expr": "datum.stage === 1 ? '' : datum.name"
}
]
},
{
"name": "stageMax",
"source": [
"n1",
"n2",
"n3"
],
"transform": [
{
"type": "aggregate",
"groupby": [
"stage"
],
"ops": [
"count"
],
"as": [
"c"
]
},
{
"type": "aggregate",
"ops": [
"max"
],
"fields": [
"c"
],
"as": [
"nMax"
]
}
]
},
{
"name": "l12",
"source": "rows",
"transform": [
{
"type": "aggregate",
"groupby": [
"s1",
"s2"
],
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"v",
"selN"
]
},
{
"type": "formula",
"as": "srcKey",
"expr": "'0|' + datum.s1"
},
{
"type": "formula",
"as": "tgtKey",
"expr": "'1|' + datum.s2"
},
{
"type": "formula",
"as": "colorName",
"expr": "datum.s1"
}
]
},
{
"name": "l23",
"source": "rows",
"transform": [
{
"type": "aggregate",
"groupby": [
"s2",
"s3"
],
"ops": [
"sum",
"sum"
],
"fields": [
"v",
"sel"
],
"as": [
"v",
"selN"
]
},
{
"type": "formula",
"as": "srcKey",
"expr": "'1|' + datum.s2"
},
{
"type": "formula",
"as": "tgtKey",
"expr": "'2|' + datum.s3"
},
{
"type": "formula",
"as": "colorName",
"expr": "datum.s3"
}
]
},
{
"name": "links",
"source": [
"l12",
"l23"
],
"transform": [
{
"type": "formula",
"as": "linkKey",
"expr": "datum.srcKey + '>' + datum.tgtKey"
},
{
"type": "lookup",
"from": "nodes",
"key": "key",
"fields": [
"srcKey"
],
"values": [
"y0",
"x0",
"rank",
"name",
"value"
],
"as": [
"sy0",
"sx0",
"srcRank",
"srcName",
"srcValue"
]
},
{
"type": "lookup",
"from": "nodes",
"key": "key",
"fields": [
"tgtKey"
],
"values": [
"y0",
"x0",
"rank",
"name",
"value"
],
"as": [
"ty0",
"tx0",
"tgtRank",
"tgtName",
"tgtValue"
]
},
{
"type": "window",
"groupby": [
"srcKey"
],
"sort": {
"field": "tgtRank"
},
"ops": [
"sum"
],
"fields": [
"v"
],
"as": [
"srcCum"
],
"frame": [
null,
0
]
},
{
"type": "window",
"groupby": [
"tgtKey"
],
"sort": {
"field": "srcRank"
},
"ops": [
"sum"
],
"fields": [
"v"
],
"as": [
"tgtCum"
],
"frame": [
null,
0
]
},
{
"type": "formula",
"as": "sy",
"expr": "datum.sy0 + (datum.srcCum - datum.v / 2) * INTERNAL_PixelsPerUnit"
},
{
"type": "formula",
"as": "ty",
"expr": "datum.ty0 + (datum.tgtCum - datum.v / 2) * INTERNAL_PixelsPerUnit"
},
{
"type": "formula",
"as": "sx",
"expr": "datum.sx0 + LAYOUT_NodeWidth"
},
{
"type": "formula",
"as": "tx",
"expr": "datum.tx0"
},
{
"type": "formula",
"as": "thick",
"expr": "max(1, datum.v * INTERNAL_PixelsPerUnit)"
},
{
"type": "formula",
"as": "ofSource",
"expr": "datum.v / datum.srcValue"
},
{
"type": "formula",
"as": "ofTarget",
"expr": "datum.v / datum.tgtValue"
},
{
"type": "linkpath",
"sourceX": "sx",
"sourceY": "sy",
"targetX": "tx",
"targetY": "ty",
"orient": "horizontal",
"shape": "diagonal"
}
]
},
{
"name": "topFlow",
"source": "links",
"transform": [
{
"type": "window",
"sort": {
"field": "v",
"order": "descending"
},
"ops": [
"row_number"
],
"as": [
"flowRank"
]
},
{
"type": "filter",
"expr": "datum.flowRank === 1"
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {
"data": "nodes",
"field": "colorKey",
"sort": true
},
"range": [
"#0f6b5c",
"#c2410c",
"#2563a8",
"#7c3aed",
"#b7791f",
"#0e7490",
"#be185d",
"#4d7c0f",
"#5b6672",
"#9a3412"
]
}
],
"marks": [
{
"type": "text",
"name": "stageHeader",
"from": {
"data": "stages"
},
"interactive": false,
"encode": {
"update": {
"x": {
"signal": "LAYOUT_StageX[datum.stage] + LAYOUT_NodeWidth / 2"
},
"y": {
"value": 14
},
"align": {
"value": "center"
},
"text": {
"signal": "upper(UI_Stages[datum.stage].label)"
},
"fontSize": {
"value": 11
},
"fontWeight": {
"value": 700
},
"fill": {
"value": "#5b6672"
}
}
}
},
{
"type": "path",
"name": "link",
"from": {
"data": "links"
},
"encode": {
"update": {
"path": {
"field": "path"
},
"stroke": {
"scale": "color",
"field": "colorName"
},
"strokeWidth": {
"field": "thick"
},
"strokeCap": {
"value": "butt"
},
"strokeOpacity": {
"signal": "STATE_HoverLink === datum.linkKey ? 0.75 : (STATE_HoverNode ? (STATE_HoverNode === datum.srcKey || STATE_HoverNode === datum.tgtKey ? 0.6 : 0.06) : (STATE_HoverLink ? 0.12 : (STATE_AnySelected ? (datum.selN > 0 ? 0.6 : 0.06) : 0.32)))"
},
"tooltip": {
"signal": "{'title': datum.srcName + ' → ' + datum.tgtName, 'Ventas': pbiFormatAutoUnit(datum.v, '#,0.0'), 'Contribución al total': format(datum.v / DATA_Total, '.1%'), 'Peso en el origen': format(datum.ofSource, '.0%') + ' de ' + datum.srcName, 'Peso en el destino': format(datum.ofTarget, '.0%') + ' de ' + datum.tgtName}"
}
}
}
},
{
"type": "rect",
"name": "node",
"from": {
"data": "nodes"
},
"encode": {
"update": {
"x": {
"field": "x0"
},
"width": {
"signal": "LAYOUT_NodeWidth"
},
"y": {
"field": "y0"
},
"y2": {
"field": "y1"
},
"fill": {
"signal": "datum.stage === 1 ? '#3e4752' : scale('color', datum.colorKey)"
},
"cornerRadius": {
"value": 2
},
"opacity": {
"signal": "STATE_HoverNode ? (STATE_HoverNode === datum.key ? 1 : 0.55) : (STATE_AnySelected && datum.selN === 0 ? 0.35 : 1)"
},
"cursor": {
"value": "pointer"
},
"tooltip": {
"signal": "{'title': datum.name, 'Ventas': pbiFormatAutoUnit(datum.value, '#,0.0'), 'Contribución al total': format(datum.share, '.1%'), 'Puesto en su etapa': datum.rank + ' de ' + datum.n}"
}
}
}
},
{
"type": "rect",
"name": "midLabelBg",
"from": {
"data": "nodes"
},
"interactive": false,
"encode": {
"update": {
"x": {
"signal": "datum.x0 + LAYOUT_NodeWidth + 4"
},
"y": {
"signal": "(datum.y0 + datum.y1) / 2 - (datum.y1 - datum.y0 >= 26 ? 15 : 9)"
},
"width": {
"signal": "datum.stage === 1 ? min(150, max(length(datum.name) * 7.2, 70)) + 8 : 0"
},
"height": {
"signal": "datum.y1 - datum.y0 >= 26 ? 33 : 18"
},
"fill": {
"value": "#ffffff"
},
"fillOpacity": {
"value": 0.82
},
"cornerRadius": {
"value": 3
}
}
}
},
{
"type": "text",
"name": "nodeLabel",
"from": {
"data": "nodes"
},
"encode": {
"update": {
"x": {
"signal": "datum.stage === 0 ? datum.x0 - 8 : datum.x0 + LAYOUT_NodeWidth + 8"
},
"y": {
"signal": "(datum.y0 + datum.y1) / 2 - (datum.y1 - datum.y0 >= 26 ? 6 : 0)"
},
"align": {
"signal": "datum.stage === 0 ? 'right' : 'left'"
},
"baseline": {
"value": "middle"
},
"text": {
"field": "name"
},
"limit": {
"signal": "datum.stage === 0 ? LAYOUT_LabelLeft - 12 : (datum.stage === 2 ? LAYOUT_LabelRight - 14 : 150)"
},
"fontSize": {
"value": 12
},
"fontWeight": {
"value": 600
},
"fill": {
"value": "#1f2933"
},
"cursor": {
"value": "pointer"
}
}
}
},
{
"type": "text",
"name": "nodeValue",
"from": {
"data": "nodes"
},
"interactive": false,
"encode": {
"update": {
"x": {
"signal": "datum.stage === 0 ? datum.x0 - 8 : datum.x0 + LAYOUT_NodeWidth + 8"
},
"y": {
"signal": "(datum.y0 + datum.y1) / 2 + 9"
},
"align": {
"signal": "datum.stage === 0 ? 'right' : 'left'"
},
"baseline": {
"value": "middle"
},
"text": {
"signal": "datum.y1 - datum.y0 >= 26 ? pbiFormatAutoUnit(datum.value, '#,0.0') + ' · ' + format(datum.share, '.0%') : ''"
},
"fontSize": {
"value": 11
},
"fill": {
"value": "#5b6672"
}
}
}
},
{
"type": "text",
"name": "annotationTopFlow",
"from": {
"data": "topFlow"
},
"interactive": false,
"encode": {
"update": {
"x": {
"signal": "LAYOUT_LabelLeft"
},
"y": {
"signal": "height - 8"
},
"align": {
"value": "left"
},
"baseline": {
"value": "bottom"
},
"text": {
"signal": "'Flujo mayor: ' + datum.srcName + ' → ' + datum.tgtName + ' · ' + format(datum.v / DATA_Total, '.0%') + ' de la venta'"
},
"limit": {
"signal": "width - LAYOUT_LabelLeft - 8"
},
"fontSize": {
"value": 12
},
"fontStyle": {
"value": "italic"
},
"fill": {
"value": "#5b6672"
}
}
}
}
]
}