1

So I have a csv file with rows of data called "opportunities.csv"

This file is in the _data folder of my Jekyll site

client,opp,person,status
Oracle,"New thing five","Mary Smith",lead
Oracle,"Data plan","Sue Curry",lead
Oracle,"Migration 2019","Sue Curry",lead
IBM, "Platform assessment","Jane Campton",lost

I'd like to display the number of leads per person:

Mary Smith    1 lead
Sue Curry     2 lead
Jane Campion  0 lead

So, for each "person" in "site.opportunities" count the number of rows for each instance of "person" where "status" = "lead"

I now this involves incrementing counters inside nested for loops but I just can't seem to get it working.

I could change the data file format to json or yml but in my current use case I have other non-technical team members editing a csv file in excel so that's a given here.

Dug Falby
  • 53
  • 5

1 Answers1

2

You can do this with group_by and where filters :

{% assign groups = site.data.opportunities | group_by: "person" %}
<ul>
{% for g in groups %}
{% assign leads = g.items | where: "status", "lead" %}
<li>name : {{ g.name }} {{ leads.size }} leads</li>
{% endfor %}
</ul>
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
  • Wow David, this is fantastic. I built your code and works perfectly. I wonder how many amazing filters like "group_by" like buried in Shopify documentation:-) Thanks very much for your answer! – Dug Falby Oct 06 '19 at 23:18
  • 1
    Here, `group_by` and `where` are Jekyll's specific filters. See https://jekyllrb.com/docs/liquid/filters/ – David Jacquel Oct 07 '19 at 10:35