Help:Count the number of property values for a page

From semantic-mediawiki.org
Semantic extension(s): Semantic MediaWiki
Further extension(s):  -/-
Keyword(s): property · ask

Description:

There is no easy way to retrieve the count of a property's values. This is a snippet you can use to count them down:

{{#ifexpr:
  {{#ask:
    [[My Page Name]]
    [[My Property Name::+]]
    |format=count
  }} > 0
  |{{#expr:
     {{#count:
       {{#ask:
         [[My Page Name]]
         [[My Property Name::+]]
         |?My Property Name=
         |mainlabel=-
         |link=none
         |format=list
       }}
       |,}} + 1
   }}
  |0
}}
Features
  • The #ifexpr part checks whether the property is even set; else returns 0.
  • if the above does not result in 0, the #expr part retrieves the property contents as a comma-separated list (you may change the comma separation to something else if necessary) and counts the occurrences of the commas. The result is increased by 1, resulting in the values' count.
Contras
  • You're using two #ask statements here. This may behave slowly if the property contains large amounts of values.